# Can we have a very simple and clear comparison example on the functions fft and nufft works?

32 views (last 30 days)
S. Gokhun Tanyer on 15 Sep 2021
Commented: S. Gokhun Tanyer on 19 Oct 2021
My task here is to compare standard fft and nufft results for a given reference function. The difference of this question from previously asked ones is to relate data with the terms 'sampling frequency' and 'sampling intervals' which both are changing for the nonuniformly spaced case. In other words, I am using a second sampling frequency to uneven the time samples.
fs=1e3; %sampling frequency for the uniform samples
ts=1/fs; %sampling interval for the uniform samples
f0 = 200; %signal frequency under test to be observed at the spectrum output.
timeDataUnif=[0:ts:1000*ts]; %uniform time samples
timeDataNunif=[0:ts:750*ts (750*ts+ts/2):ts/2:1000*ts]; % nonuniform samples for the same time period with twice the sampling frequency
nData1=length(timeDataUnif);
nData2=length(timeDataNunif);
sigDataUnif=sin(2.*pi.*f0.*timeDataUnif) +0.01.*rand(1,nData1);
sigDataUnif=sigDataUnif-sum(sigDataUnif)/nData1; %optionally removing the DC component
fSigData=fftshift(fft(sigDataUnif)); %spectrum of the uniform samples
freqDataUnif=[0:nData1-1]./(nData1-1).*fs-fs/2; %uniform frequency samples
freqDataNunif=[0:nData2-1]./(nData2-1).*(2.*fs)-fs; %uniform frequency grids for twice fs.
sigDataNunif=2*sin(2.*pi.*f0.*timeDataNunif) +0.01.*rand(1,nData2);
nufSigData=fftshift(nufft(sigDataNunif,timeDataNunif));
figure;
subplot(411),plot(timeDataUnif,'k.');hold on;
subplot(411),plot(timeDataNunif,'r.');title('uniform and nonuniform samples of 1 second time interval');xlabel('sample number');axis tight;grid on;grid minor;
subplot(412),plot(timeDataUnif,sigDataUnif,'k');hold on;title('different samples of the same function');
subplot(413),plot(freqDataUnif,abs(fSigData),'k');hold on;axis tight;V=axis;axis([V(1) V(2) -20 V(4)]); grid on;grid minor;
subplot(412),plot(timeDataNunif,sigDataNunif,'r--');hold on;title('FFT of the uniform samples');
subplot(414),plot(freqDataNunif,abs(nufSigData),'k');title('NUFFT of the nonuniform samples');grid on;grid minor; Chris Turnes on 19 Oct 2021
The key thing to remember is that since nufft takes the sample points and frequency points as arguments, you have to remember how to manage their units and how they are defined relative to one another.
The formula for the classical DFT is and typically we assume that each sample is defined as where Ts is your sampling period and x() is your analog signal (note that I'm assuming MATLAB's 1-based indexing here, hence the -1 factors). Each represented DFT frequency corresponds to the sampled analog frequency of the spectrum so that X[k] corresponds to the spectral content at an analog frequency .
The generalized NUFFT equation is In a call to nufft where you are not providing frequency points, the assumed points take the form of Therefore, if you want results on a comparable scale, you have to scale your sample data to range from 0:(N-1):
fs=1e3; %sampling frequency for the uniform samples
ts=1/fs; %sampling interval for the uniform samples
f0 = 200; %signal frequency under test to be observed at the spectrum output.
timeDataUnif=[0:ts:1000*ts]; %uniform time samples
timeDataNunif=[0:ts:750*ts (750*ts+ts/2):ts/2:1000*ts]; % nonuniform samples for the same time period with twice the sampling frequency
nData1=length(timeDataUnif);
nData2=length(timeDataNunif);
sigDataUnif=sin(2.*pi.*f0.*timeDataUnif) +0.01.*rand(1,nData1);
sigDataUnif=sigDataUnif-sum(sigDataUnif)/nData1; %optionally removing the DC component
fSigData=fftshift(fft(sigDataUnif)); %spectrum of the uniform samples
freqDataUnif=[0:nData1-1]./(nData1-1).*fs-fs/2; %uniform frequency samples
freqDataNunif=[0:nData2-1]./(nData2-1).*(2.*fs)-fs; %uniform frequency grids for twice fs.
sigDataNunif=2*sin(2.*pi.*f0.*timeDataNunif) +0.01.*rand(1,nData2);
% ------ Divide by ts here to set the correct scaling.
nufSigData=fftshift(nufft(sigDataNunif,timeDataNunif/ts));
% ------
figure;
subplot(411),plot(timeDataUnif,'k.');hold on;
subplot(411),plot(timeDataNunif,'r.');title('uniform and nonuniform samples of 1 second time interval');xlabel('sample number');axis tight;grid on;grid minor;
subplot(412),plot(timeDataUnif,sigDataUnif,'k');hold on;title('different samples of the same function');
subplot(413),plot(freqDataUnif,abs(fSigData),'k');hold on;axis tight;V=axis;axis([V(1) V(2) -20 V(4)]); grid on;grid minor;
subplot(412),plot(timeDataNunif,sigDataNunif,'r--');hold on;title('FFT of the uniform samples');
subplot(414),plot(freqDataNunif,abs(nufSigData),'k');title('NUFFT of the nonuniform samples');grid on;grid minor; S. Gokhun Tanyer on 19 Oct 2021
Thank you for your excellent answer. It is now all clear. Thank you.

R2020b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!