Need help with zero-padding impacts interpretation

11 次查看(过去 30 天)
I'm doing a project where I need to provide an analysis of zero padding impacts by using the sum of sinusoids sampled at 5 kHz vary the frequency spacing of the sinusoids and show DFT of lengths, 256, 512, 1024, and 4096, using the window sizes of 256, and 512, Assume a rectangular window.
Which means DFT size is larger than window size, and we are zero-padding the samples.
I got these two figures from my code, but I don't know how to interpret the impacts of zero-padding. It seems that at window size of 256, no matter how you increase DFT size, the results are always not distinguishable between two peaks of sinusoids. While my instructor said frequency accuracy depends on window size, and frequency resolution depends on DFT size. But here when window size is too small, we can't distinguish between peaks even the resolution is small.
Here is my code:
%Part 5
% MATLAB code to analyze zero-padding in the DFT using a rectangular window
fs = 5000; % Sampling frequency (5 kHz)
t_duration = 1; % Signal duration in seconds
t = 0:1/fs:t_duration-1/fs; % Time vector
% Window sizes to analyze
window_sizes = [256, 512];
% Zero-padded DFT sizes to analyze
N_dft = [1024, 2048, 4096];
% Frequencies of the sum of sinusoids (vary frequency spacing)
f1 = 1000; % Frequency of the first sinusoid (1 kHz)
f_spacing = [5, 10]; % Frequency spacing between the two sinusoids
f_end = f1 + f_spacing; % Frequency of the second sinusoid
% Prepare figure
for window_size = window_sizes
figure; % Create a new figure for each window size
hold on;
for N = N_dft
for spacing = f_spacing
f2 = f1 + spacing; % Second sinusoid frequency
% Generate the sum of two sinusoids with frequencies f1 and f2
x = sin(2*pi*f1*t) + sin(2*pi*f2*t);
% Apply rectangular window (by taking the first window_size samples)
x_windowed = x(1:window_size); % Select the first window_size samples
% Zero-pad the signal if DFT size is larger than window size
x_padded = [x_windowed, zeros(1, N - window_size)];
% Generate DFT matrix for size N using dftmtx
DFT_matrix = dftmtx(N);
% Manually compute the DFT using the DFT matrix
X = DFT_matrix * x_padded(:); % Compute DFT of the windowed and zero-padded signal
% Compute the frequency axis for the current DFT
freq_axis = (0:N-1)*(fs/N);
% Plot the magnitude of the DFT
plot(freq_axis, abs(X), 'DisplayName', ['Spacing = ', num2str(spacing), ' Hz, N = ', num2str(N)]);
end
end
% Add labels and legend
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title(['Zero-Padded DFT Magnitude Spectrum (Window Size = ', num2str(window_size), ')']);
legend('show');
grid on;
hold off;
xlim([f1-10, f2+10])
end

回答(2 个)

dpb
dpb 2024-10-8
编辑:dpb 2024-10-8
Compare each to the theoretical; the pointwise values are identical at the same frequency bin regardless...you might also want to investigate the effect of changing the number/sampling rate to not necessarily match the signal frequencies...

Shivam Gothi
Shivam Gothi 2024-10-9
Hello @Zhen
As per my understanding, you are attempting to plot the DFT of a signal for various window widths and DFT sizes, but you're unsure how to interpret the results. The code you provided is well-written, and the results it produces are accurate. I'll attempt to interpret these results by offering some basic examples. Let's tackle your points one by one.
  1. The first statement:
frequency accuracy depends on window size
For instance, let's consider a very simple input signal with a constant magnitude over the entire time range from- to +. This is shown in the below figure.
The Discrete time Fourier transform of this signal is represented by an impulse centered at the origin, as depicted in figure (Fig 2).
In practical applications, we aim to process this input signal, but taking infinite samples is not feasible. Therefore, we first pass the input signal through a window of finite length. Let's consider a simple rectangular window with a size of (N=5). When the signal from (Fig 1) is passed through this window, the resulting output is shown in (Fig 3). (Remember: Passing a signal through a window means multiplication in time domain. And, multiplication in time domain corresponds to the convolution in frequency domain).
Now, the Fourier transform of the windowed signal will look like as shown in the below figure:
We can see that there is significant difference in the Fourier transform of original signal (fig2) and the Fourier transform of windowed signal (fig4). Our aim is to select such a window that will try its best to preserve the frequency response of the original signal. That's why we have windows like the 'Hanning window', 'Kaiser window' etc, which are optimized to meet specific requirements. However, let's not delve into those details at this moment.
However, if we increase the window size to (N=20), you can experiment with this yourself and observe that the Fourier transform will contract, more closely resembling the Fourier transform of the original signal (as shown in Fig 2).
As a conclusion, if we choose a larger window size, the Fourier transform of windowed signal closely resembles the Fourier transform of original signal, and thus preserving the “frequency response information” or, in other words, it yields more accurate frequency response. This justifies the statement “frequency accuracy depends on window size. Here, I have taken a very simple input example to illustrate the concept. But this holds true for any other input signal.
Now let us address the second statement:
frequency resolution depends on DFT size
We have a finite duration signal, obtained by passing the input signal through the rectangular window. Now we want to perform DFT (Discrete Fourier Transform) on it. The DFT size should be greater than the signal size. Below given figure compares the results when we perform DFT on a finite length signal for different DFT sizes.
It is clear that as we increase the DFT size, we get more closer samples of frequency. That is we get a better frequency resolution. Hence, it justifies the second statement
Conclusions from the plots attached by you:
We can draw the following conclusions from the plots attached by you:
  1. Case-1: Window size (N = 256), DFT size = 1024, 2048, 4096
  • As the window size is less, the DFT is not accurate. But, as you increase the DFT size, it is evident that the frequency resolution is getting improved.
  1. Case -2: Window size (N = 512), DFT size = 1024, 2048, 4096
  • As the window size is greater than the previous case, DFT is accurate (has less error). As you increase the DFT size, it is evident that the frequency resolution is getting improved.
Therefore, Accuracy refers to “how much close you are from the actual response” and resolution refers to : “how many samples of frequency are there in DFT”
Therefore, in order to get better accuracy and frequency resolution, window size and the DFT size, both should be increased.
"Please note that in your plot, the discrete points of the DFT are connected by straight lines. As a result, the DFT does not resemble the one shown in Figure 5."
I hope the justification is as per your expectation!

产品

Community Treasure Hunt

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

Start Hunting!

Translated by