0
$\begingroup$

So, suppose you have a superposition of cosines of the same magnitude: $\sum_{i}\cos[\omega_i t]$

When you take the Fourier transform, theoretically you'd expect to find $\delta$-like peaks of the same magnitude. But in practice, what I find is this:

enter image description here

How would you explain this variation in the magnitude? Given such variation, would it be possible to recover the amplitudes of each frequency component in a superposition $\sum_{i}A_i\cos[\omega_i t]$

EDIT: I suspect it has to do with the relationship between the sampling frequency and the frequencies of the components. The fact that the theoretical $\delta$ response exists is a consequence of the orthogonality of the FT kernel. So, I suppose that in the discrete case, the frequencies of the DFT lines don't exactly "match" the sampled frequencies of the signal. I don't know how to articulate this properly or if it's even correct though.

For reference, here's the MATLAB code I used to generate this

Fs = 100;
Sp = 1/Fs;
L = 2000;
t = (0:L-1)*Sp;

y2 = cos(5.*t)+cos(25.*t)+cos(10.*t)+cos(1.*t)+cos(100.*t)+cos(200.*t)+cos(300.*t);

Y = fft(y2);
P2 = abs(Y/L).^2;
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = Fs*(0:(L/2))/L;

figure
plot(f,P1,'-k','Linewidth',1);

$\endgroup$
  • $\begingroup$ Notice that the image changes if you select a different amount of points in L. Take care of the FFT bin resolution. You should also not ignore this warning Matlab is throwing: Warning: Integer operands are required for colon operator when used as index. $\endgroup$ – VMMF Apr 17 '18 at 15:24
  • $\begingroup$ Yeah L should be 2000, forgot to change it. Thanks. $\endgroup$ – user396072 Apr 17 '18 at 15:29
0
$\begingroup$

I have a blog that concentrates heavily on DFTs. The primary concept you have to grasp is that it is different than the continuous so your "expectations" are incorrect. The key values are not the sampling rate and frequency in Hz, but how many sample points are in the frame and what is the frequency in terms of cycles per frame. When there are a whole number of cycles in a frame, the DFT behaves as you are expecting. When the frequency isn't a whole number, the DFT behaves quite differently than the continuous case. The "energy" of the signal is "spread" near the bin you expect. This spread is called "leakage". It is possible to recover the frequency of the tone from the values of the leakage.

These are the articles I've written on finding the frequency of a pure real tone in a DFT:

Exact Frequency Formula for a Pure Real Tone in a DFT

Two Bin Exact Frequency Formulas for a Pure Real Tone in a DFT

Improved Three Bin Exact Frequency Formula for a Pure Real Tone in a DFT

You will find a lot more literature on frequency estimation than amplitude and phase calculations. My blog article, Phase and Amplitude Calculation for a Pure Real Tone in a DFT: Method 1, gives a way to calculate an exact answer for a single tone and a good estimate in the presence of other tones and noise.

You can get much better values in a multiple tone situation by removing the effects of the other tones from the DFT and recalculating the values for each tone in an iterative manner. Formulas for the direct calculation of the other tones' DFT values can be found in my articles DFT Bin Value Formulas for Pure Real Tones and An Alternative Form of the Pure Real Tone DFT Bin Value Formula.

A blog article detailing this is in the pipeline. For the case you site, a mix of pure cosines, my method will separate all the tones out and give you exact answers.

Hope this helps.

Ced

$\endgroup$
0
$\begingroup$

This question is well-addressed in other posts; I would refer you elsewhere, but since you probably want the answer, here it is.

A couple of notes:

  1. There are three knobs you need to play around with to get the right FFT. The first knob is sampling enough (so the 1/700 parameter); in particular, if you don't do it at least twice the maximum frequency, you run into problems of aliasing (by far the best and easiest to understand resource is here: https://blogs.mathworks.com/steve/category/fourier-transforms/). The second knob is that you need to sample long enough (the 50 number below) - you could do 1/1000 sampling but if you do up to 10 - 1/1000 and do the FFT, you will notice the amplitude of the smallest frequency (1/(2 * pi)) is always a little too high.

  2. The linked blog explains VERY well why this third knob is important, which is the zero-padding, which allows you to get a better DFT with the same data.

  3. Finally, yes I am plotting the two-sided FFT, so usually people will take half the amplitude I am using: $$\text{plot}(\text{fshift},\text{abs}(\text{yshift})/\text{length}(x))$$ instead of $$\text{plot}(\text{fshift},2 * \text{abs}(\text{yshift})/\text{length}(x))$$.

But this recreates the right amplitudes. The idea is you need to normalize by the number of data points you are using (I didn't come up with this code myself; I stole it from here: https://www.mathworks.com/matlabcentral/answers/162846-amplitude-of-signal-after-fft-operation).

Without further ado, here's the code you want:

\begin{equation} t = 0:1/700:50-1/700; \\ x = cos(5.*t)+cos(25.*t)+cos(10.*t)+cos(1.*t)+cos(100.*t)+cos(200.*t)+cos(300.*t); \\ y = \text{fft}(x, 2^{\text{nextpow2}(\text{length}(t))+2)}); \\ n = \text{length}(y); \\ \text{fshift} = (-n/2:n/2-1)*(700/n); \\ \text{yshift} = \text{fftshift}(y); \\ \text{figure}; \text{plot}(\text{fshift},2 * \text{abs}(\text{yshift})/\text{length}(x)) \\ \end{equation}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.