Why does fft of non-integer number of cycles sin wave NOT have sidelobes?
13 views (last 30 days)
Show older comments
As per the title, I have a simple sin wave at a single frequency, and am cutting it off at some time t such that the sin wave is cut off at a non-integer number of cycles. I would have expected that cutting it off at random times would result in a significant number of sidelobes when computing the fft, however this is not the case, typically I am only seeing a single peak at the frequency of the sin wave. In another programme which calculates the fourier transform of the signal I am seeing side lobes. My question is, why am I not seeing side lobes, is the MATLAB fft algorithm doing something clever?
Here is the code I am using:
N=7500;
f0 = 0.25*10^9;
blerg(:,1) = linspace(0,100*10^-9,N+1);
blerg(:,2) = sin(2*pi*f0*blerg(:,1));
input = blerg;
dt = (input(2,1)-input(1,1))
N = length(input);
Fs = 1/dt;
spectLen = isEven(N);
freqAxis = linspace(0,Fs/2,spectLen);
Y = fft(input(:,2))/N;
Ydb = mag2db(abs(Y));
plot(freqAxis/10^6,Ydb(1:spectLen));
xlim([0 1000]);
0 Comments
Answers (2)
Matt J
on 15 May 2025
Edited: Matt J
on 15 May 2025
We don't know what you're doing since we haven't been shown your code. However, here is an example to show that you won't get perfect peaks in general.
m=5;N=5000;
f0=0.5;
df=f0/m;
dt=1/N/df; %duration of sine wave = m/f0
t=(0:N-1)*dt;
y=sin(2*pi*f0*t);
y(t>m/f0*0.7)=0; %cut off
M=2*N;
fAxis = ((0:M-1)-ceil((M-1)/2))*df*N/M;
plot(t,y)
plot(fAxis,fftshift(abs(fft(y,M)))*dt,'--.'); xlim([-3,+3]*f0)
5 Comments
Matt J
on 15 May 2025
Edited: Matt J
on 15 May 2025
I thought that a sin wave that does not end at zero after an integer number of cycles will cause side lobes due to the assumed periodicity of the window over which you take a Fourier Transform,
It will, but this isn't the Fourier Transform. It's the Discrete Fourier Transform, which means you won't see the peak or the side lobes if you don't make sure frequency is discretized at the right locations. If your wave samples do not run for complete periods, it means again that your freqAxis sample points do not include f0. As for the side lobes, it is possible that you are sampling their zero-crossings only, instead of their peaks. We have to check at what your frequency sampling interval really is.
Matt J
on 15 May 2025
Edited: Matt J
on 15 May 2025
So, from the output below, it looks like your sine wave runs for almost exactly 25 periods. Since this is only slightly noninteger, It doesn't seem surprising that side lobes are absent. You can see how that changes when I insert truncation manually:
N=7500;
f0 = 0.25*10^9;
blerg(:,1) = linspace(0,100*10^-9,N+1);
blerg(:,2) = sin(2*pi*f0*blerg(:,1));
input = blerg;
dt = (input(2,1)-input(1,1))
N = height(input);
Fs = 1/dt;
df=1/N/dt
numberOfPeriods = N*dt*f0
%plot(input(:,1), input(:,2))
spectLen=floor(N/2);
freqAxis = (0:spectLen-1)*df;
Y = fft(input(:,2))/N;
Ydb = mag2db(abs(Y));
inTrunc=input(:,2); inTrunc(round(2*end/3):end)=0;
Ydbtrunc= mag2db(abs(fft(inTrunc)/N));
plot(freqAxis/1e6, Ydb(1:spectLen) , freqAxis/1e6, Ydbtrunc(1:spectLen),'--');
xlim([0 1000]); legend Given Truncated
Image Analyst
on 15 May 2025
As you know, the FT of an infinitely long pure sine wave is a delta function. However you don't have an infinitely long sine wave, you have a certain limited number of cycles in a window. So essentially your signal is a sine wave multiplied by a rect function. Now you know that the FT of a rect function is a sinc function, and the FT of two functions multiplied by each other is the convolution of the two FTs. A delta function convolved with anything is that thing, so the FT of the sine (the delta function) convolved with the FT of a rect function (the sinc) will just be sinc function. And as you know (or should know) the wider the rect function, the narrower the sinc function will be. So with many many cycles of the sine inside the rect, you will get a very narrow spectrum, where the side lobes might not be very well resolved. The sinc width varies inversely with the rect function width. If you make your rect function narrower to include fewer cycles, the sinc function will widen, and the side lobes will be more noticeable because of that.
0 Comments
See Also
Categories
Find more on Time-Frequency Analysis 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!