Add zero padding to fft

213 次查看(过去 30 天)
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

采纳的回答

Star Strider
Star Strider 2024-4-1
编辑:Star Strider 2024-4-2
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 个评论
S
S 2024-4-5
@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?
Star Strider
Star Strider 2024-4-5
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 ).

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Fourier Analysis and Filtering 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by