Add zero padding to fft

I am trying to add zero padding to my fft but am unsure where I would add it, I have tried putting it at the end of the fft_spectrum line but that isn't working. Thank you for your time!
%% perform FFT of signal :
[freq,fft_spectrum] = do_fft(t,output_sum);
figure
plot(freq,fft_spectrum,'-*')
xlim([0 1000]);
title('FFT of output sum signal')
ylabel('Amplitude');
xlabel('Frequency [Hz]')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [freq_vector,fft_spectrum] = do_fft(time,data)
time = time(:);
data = data(:);
dt = mean(diff(time));
Fs = 1/dt;
nfft = length(data); % maximise freq resolution => nfft equals signal length
%% use windowing or not at your conveniance
% no window , zero padd!!!
fft_spectrum = abs(fft(data))*2/nfft;
% % hanning window
window = hanning(nfft);
window = window(:);
fft_spectrum = abs(fft(data.*window))*4/nfft;
% one sidded fft spectrum % Select first half
if rem(nfft,2) % nfft odd
select = (1:(nfft+1)/2)';
else
select = (1:nfft/2+1)';
end
fft_spectrum = fft_spectrum(select,:);
freq_vector = (select - 1)*Fs/nfft;
end

 Accepted Answer

Let the fft function add it to the original time domain signal vector (it pads it at the end) by specifying the second argument to be greater than the signal length.
Using:
NFFT = 2^nextpow2(L);
where ‘L’ is the signal length is best, because lengths of powers-of-2 (not limited to ‘L’, and can be several times that length so long as it is a power-of-2 and fits in memory) really is more efficient and faster (I actually timed it a while back). This also increases the frequency resolution, always a good thing (in my opinion).
As for calculating the one-sided Fourier transformm, I coded ‘FFT1’ a while back for my own use, and have posted it it here in some of my answers —
Fs = 44100;
L = 1;
t = linspace(0, Fs*L, Fs*L+1).'/Fs; % Time Vector (Assume Column Vectors)
s = sin(2*pi*t*(1:1000:2.2E4)); % Signal Vector Matrix
[FTs,Fv] = FFT1(s,t); % Call 'FFT1' Function
figure
plot(Fv, abs(FTs)*2) % Plot Results
grid
xlim([0 max(Fv)])
axis('padded')
xlabel('Frequency (Hz)')
ylabel('Magnitude')
function [FTs1,Fv] = FFT1(s,t)
t = t(:);
L = numel(t);
if size(s,2) == L
s = s.';
end
Fs = 1/mean(diff(t));
Fn = Fs/2;
NFFT = 2^nextpow2(L);
FTs = fft((s - mean(s)) .* hann(L).*ones(1,size(s,2)), NFFT)/sum(hann(L));
Fv = linspace(0, 1, NFFT/2+1)*Fn;
Iv = 1:numel(Fv);
FTs1 = FTs(Iv,:);
end
The online Run feature is still not working (it didn’t work all day yesterday either), so I can’t demonstrate it here, however I again verified that it works (in MATLAB Online).
EDIT — (2 Apr 2024 at 10:37)
The Run feature is back, so I ran the code.
.

4 Comments

This makes sense, but in my case if I did not want to change my functions, is there a way to just change this line to add zero padding:
plot(freq,fft_spectrum,'-*')
Or change something in the top section where I am doing the fft?
Add the zero-padding in the appropriate place in your ‘do_fft’ function. Adding it in the plot call simply does not make sense.
If you want to add zero-padding, change this line:
fft_spectrum = abs(fft(data.*window))*4/nfft;
to:
fft_spectrum = abs(fft(data.*window),nfft))/sum(window);
similar to my code. (To normalise, divide by the signal length if you are using no window function or a rectangular window, since those are the same thing, and by the sum of the window function otherwise.)
Also, note that by calculating the absolute value in computing your ‘fft_spectrum’ veriable, you elliminiate the possibility of calculating the phase if you need to at some later time. Returning the complex vector or matrix instead allows that (using the angle function).
@Star Strider oh ok! wait so I'm trying to understand windowing vs zero padding.
Zero padding is adding zeros to the beginning and end to better resolution and windowing calculates data values within a certain window to improve accuracy? Do I have that right?
In MATLAB, zero-padding is usually done at the end of the vector. (Zero-padding to an integer power-of-2 increases the fft efficiency because the fft algorithm works best in that instance.) It has the definite additional advantage of increasing the frequency resolution.
Windowing (for example with a hann window) corrects for the finite nature of a discrete Fourier transform, so the result more closely matches the result of an analytic Fourier transform (that by definition integrates from .to ).

Sign in to comment.

More Answers (0)

Categories

Find more on Fourier Analysis and Filtering in Help Center and File Exchange

Asked:

S
S
on 1 Apr 2024

Commented:

on 5 Apr 2024

Community Treasure Hunt

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

Start Hunting!