-1
$\begingroup$

Let us consider the following code:

function [ x ] = generate1(N,m,A3)
f1 = 100;
f2 = 200;
T = 1./f1;
t = (0:(N*T/m):(N*T))'; %'
wn = randn(length(t),1); %zero mean variance 1
x = 20.*sin(2.*pi.*f1.*t) + 30.*cos(2.*pi.*f2.*t) + A3.*wn;
%[pks,locs] = findpeaks(x);
 %plot(x);
end

I know peaks in the Fourier domain are represented at these frequencies, which are present in the signal.

For example, let us take the plot of the Fourier transform of this signal:

y=generate1(3,500,1);

and plot

plot(abs(fft(y)))

enter image description here

but clearly it does not show peaks at the frequencies given in the signal. What is the problem?

$\endgroup$
14
  • $\begingroup$ any help please? $\endgroup$
    – user350
    Commented Jan 4, 2014 at 7:23
  • $\begingroup$ i can't understand what is problem there,because in my mind it should work without problem $\endgroup$
    – user350
    Commented Jan 4, 2014 at 9:33
  • $\begingroup$ But it looks exactly like it should. You are sampling a signal with frequencies 100Hz and 200Hz with a sampling frequency of 16.66kHz (represented by 500 in your diagram). So your signal has a rather low frequency relative to the sampling frequency. So your components should be at 6 and 12, which seems to be the case in the plot. $\endgroup$ Commented Jan 4, 2014 at 9:34
  • $\begingroup$ but peaks must be at frequency 100 and 200 is not it? $\endgroup$
    – user350
    Commented Jan 4, 2014 at 9:36
  • $\begingroup$ yes i wannted to avoid aliasing,but maybe i made some mistake? $\endgroup$
    – user350
    Commented Jan 4, 2014 at 9:40

1 Answer 1

1
$\begingroup$

Lets analyze the computation. Your function takes $N$ periods of the $100Hz$ sine wave and correspondingly $2N$ periods of the $200Hz$ sine wave and samples them in $m$ equidistant points.

Then you perform an FFT of size $m$. If we forget for the moment that there is a factor of 100Hz used in the production, we have sampled waves of $N$ and $2N$ full oscillations, and the FFT detects or handles sinusoids with up to $m/2$ full oscillations.

In the plot from of the FFT result scaled from 0 to m one would expect the first wave at position N, the second at position 2N, and the mirror frequencies at m-1-N and m-1-2N.

In your example with N=3 and m=500, the 100Hz component is at position 3 and the 200Hz component at position 6.

To get more striking results, either reduce the range in the display to $[0:10N]$ or similar, or choose the magnitudes of m and N closer together, m=8N to m=20N should work well.


Added matlab interna: the fft command produces the DC at position 1, the first array position. The FFT with sampling frequency $f_s$ and $m$ samples produces a sampling of the frequency spectrum ranging from $-f_s/2$ to $f_s/2$ in $m$ steps, assuming $m$ is even.

The fft command arranges these frequencies by wrapping around at $fs/2$, so that a component for $f>fs/2$ in reality is a component for $f-f_s<0$. For real signals, the negative spectrum is essentially the same as the positive, complex conjugation is the only difference. So one can just disregard the second half of the result of fft for visualization purposes.

The generate1 function should at least also return the sampling frequency fs=(f1*m)/N. Then

m=500;
y,fs=generate1(3,m,1);
A=abs(fft(y))/m;
f=(0:1:m-1)*fs;
plot(f(1:m/2),A(1:m/2))

produces the full spectrum with correct scale. Replace m/2 in the plot command with a smaller number to display only parts of the spectrum.

$\endgroup$
10
  • $\begingroup$ in ploting how should i use [0:10N]? $\endgroup$
    – user350
    Commented Jan 4, 2014 at 9:57
  • $\begingroup$ matlab has not indexed 0 or negative $\endgroup$
    – user350
    Commented Jan 4, 2014 at 10:00
  • $\begingroup$ i want to show peaks exactly at 200 and 100 $\endgroup$
    – user350
    Commented Jan 4, 2014 at 10:01
  • $\begingroup$ Since position 1 is frequency 0, shift everything by 1. From what I wrote, you can set N=100, m=1000, or introduce a proper frequency scale f=(0:1:m-1)*100.0/N to then plot(f(1:10*N), A(1:10*N)). $\endgroup$ Commented Jan 4, 2014 at 10:08
  • $\begingroup$ will not it be too much sampling frequency? $\endgroup$
    – user350
    Commented Jan 4, 2014 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.