FFT of a single sinusoid showing noise ?!
11 views (last 30 days)
Show older comments
Hi,
I have a signal which I am sampling so at least 10 wavelengths or 10 time periods can be plotted. It then works out how many samples are required based on the sample frequency.
I have done a FFT quite a few times but this is somehow showing unwanted frequency components on a pure sinusoidal signal...
clear all
f = 30 %Carrier freqency
Fs = 20* f; %Sample frequency atleast 2*f
Ts = 1/Fs; %Sample time
%Number of points must be a factor of 2^n and be greater than the number of
%points required for the wanted number of time periods on the plot
plot_periods_of_f = 10;
min_num_samples = (plot_periods_of_f * (1/f)) / (Ts)
%Make sure the number of samples, Ns, is greater than min_num_samples
a=1
while 2^a < min_num_samples
a = a+1;
Ns = 2^a;
end
n = (0:Ns-1)*Ts; %Time vector
sig = cos(2*pi*f*n );
%Setup FFT
N=Ns;
freq_domain = (0:N/2); %Show positive frequency only
freq_domain = freq_domain * Fs / N;
%FFT
ft_raw = fft(sig)/N;
ft = 2*abs(ft_raw); % 2* to compensate for negative frequency energy
ft = ft(1:N/2+1); %Show positive frequency only so it matches the setup of frequency domain
%plot
figure(1);cla;clf
subplot(2,1,1)
plot(n,sig)
subplot(2,1,2);
bar(ft)
For some odd reason my fft is showing noise and I cant seem to work out why... what have I done incorrectly? I thought its all okay..
4 Comments
Adam
on 22 Oct 2018
The fft assumes an infinite signal. You would only get a pure dirac in the frequency domain if you could run it on an infinite sine wave, which you can't. The truncation means that you are running it on an approximation instead, which will be zero to plus/minus infinity.
Your script below also has leakage, it is just a lot smaller because your sine wave has a lot more cycles so is closer to an infinite sine wave (though obviously still not one).
Answers (2)
Matt J
on 22 Oct 2018
Edited: Matt J
on 22 Oct 2018
Looks normal to me, too. Aside from the fact that the sinusoid is discrete, it is also a truncated, non-periodic sampling, so there's no reason to expect only a single frequency component.
10 Comments
Matt J
on 22 Oct 2018
I have been using this previously and it works for all frequencies
It doesn't. For example,
Fs =2^8;
Ts = 1/Fs;
Ns = (2^12);
n = (0:Ns-1)*Ts;
s=cos(2*pi*(Fs/567)*n);
figure(1);
plot(n,s);
figure(2);
F=abs(fft(s));
bar(F/max(F))
xlim([0,100]);
ylim([0,.3])
title 'Normalized Amplitude Response'
Matt J
on 22 Oct 2018
Edited: Matt J
on 22 Oct 2018
Maybe this is what you're looking for
%%User-selected parameters
f=30; %Frequency of sinusoid
M=5; %number of periods of the sinusoid
Fs=2^8; %Number of samples per period
%%Generate signal
T=M/f; %Total duration of signal - M periods
n=linspace(0,T,M*Fs+1); %sample times
n(end)=[];
Ts=n(2)-n(1);
s = cos(2*pi*f*n);
%%Plot
figure(1);
plot(n,s);
figure(2);
F=abs(fft(s));
bar(F/max(F))
xlim([0,100]);
ylim([0,.3])
title 'Normalized Amplitude Response'
shg
Bruno Luong
on 22 Oct 2018
Edited: Bruno Luong
on 22 Oct 2018
FFT/DFT returns components of the sins/cosine function that are exactly periodic with respect to the number of input data points. You can see as circular spectrum.
If you feed FFT with an input sig with a mono frequency but that is not exactly one of those periodic frequencies, meaning your signal when wrap around has a jump (~ sig(end)-sig(1)), the spectrum will stretch out to accommodate to the jump (similar to a continuous transform of the heaviside fct).
This is not "noise", but miss-understanding of the fundamental of DFT (by too many people unfortunately).
0 Comments
See Also
Categories
Find more on Transforms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!