0
votes

I do np.fft.fft() to decompose discrete-signal to frequencies. Then I pick top N freqs and now I want to draw a signal of the sum of those frequencies using the following formula :

 amp * np.sin( 2 * np.pi * freq * time + phase )

I extract the freqs based on their position in the array (fftfreqs), amplitudes with np.abs(complex_num) and phases with np.angle(complex_num, deg=True), from the data I got from from fft() call.

It does not seem to work very well.

What I'm doing wrong ? Do I understand correctly that the complex-num contains the amp&phase of sinusoid ? Do I have to use sin&cos for the drawing ? The documentation is very sparse on exact interpretation of the result of fft().

PS> I know I can use np.fft.ifft() to draw close approximation, but this way of doing it is limited to the time-frame of the original signal. I want to draw/fit the original signal with the top N freqs in the original timeframe, but also to draw the "continuation" of the signal after this timeframe using those freqs i.e. the extended signal.

1
The approach seems plausible. The deg=True parameter normally does not play well with sin(). Posting some runnable code would ease to investigate what is not working well.Dietrich
please show us the plots...ederwander

1 Answers

3
votes

I found my mistakes.. first the correct way to calculate the waves is (sum of those for topN freqs) :

cos_amp * np.cos( 2 * np.pi * freq * time) + sin_amp * np.sin( 2 * np.pi * freq * time)

not just sin()..second it is incorrect to use abs-amplitude and angle-phase extraction from the complex array. The real part contains the cos-amp and the negative-imaginary part contains the sin-amplitude i.e. once I get the complex array out of fft(), I do something like this :

fft_res = np.fft.fft(signal)
.... topN_freqs_idx = find top freqs from PSD
all_freqs = np.fft.fftfreq(N)
freqs = all_freqs[topN_freq_idx]
norm = fft_res[topN_freq_idx] / (N/2)
cos_amp = np.real(norm)
sin_amp = -np.imag(norm)

I figured out this from here : http://www.dspguide.com/ch8/5.htm