Accelerating the pace of engineering and science

# Documentation

## Amplitude Estimation and Zero Padding

This example shows how to use zero padding to make an accurate estimate of the amplitude of a sinusoidal signal. Frequencies in the discrete Fourier transform (DFT) are spaced at intervals of Fs/N where Fs is the sampling frequency and N is the length of the input time series. Attempting to estimate the amplitude of a sinusoid with a frequency that does not correspond to a DFT bin can result in an inaccurate estimate. Therefore, zero padding the data before you obtain the DFT can often increase the frequency resolution and thus improve the amplitude estimate.

Create a signal consisting of two sine waves. The two sine waves have frequencies of 100 and 202.5 Hz. The sampling frequency is 1000 Hz and the signal is 1000 samples in length.

```Fs = 1e3;
t = 0:0.001:1-0.001;
x = cos(2*pi*100*t)+sin(2*pi*202.5*t);```

Obtain the DFT of the signal. The DFT bins are spaced at 1 Hz. Accordingly, the 100-Hz sine wave corresponds to a DFT bin, but the 202.5-Hz sine wave does not.

Because the signal is real-valued, use only the positive frequencies from the DFT to estimate the amplitude. Scale the DFT by the length of the input signal and multiply all frequencies except 0 and the Nyquist by 2.

Plot the result with the known amplitudes for comparison.

```freqres = Fs/length(x);
xdft = fft(x);
xdft = xdft(1:length(x)/2+1);
xdft = 1/length(x).*xdft;
xdft(2:end-1) = 2*xdft(2:end-1);
freq = 0:Fs/length(x):Fs/2;
plot(freq,abs(xdft));
xlabel('Hz'); ylabel('Amplitude');
h = line(freq,ones(length(x)/2+1,1));
set(h,'color',[1 0 0],'linewidth',2);```

The amplitude estimate at 100 Hz is accurate because that frequency corresponds to a DFT bin. However, the amplitude estimate at 202.5 Hz is not accurate because that frequency does not correspond to a DFT bin.

You can interpolate the DFT by zero padding. While zero padding does not improve the frequency resolution of the DFT, zero padding can allow you to obtain more accurate amplitude estimates of resolvable signal components.

Pad the DFT out to length 2000. With this length, the spacing between DFT bins is Fs/2000=0.5 Hz. In this case, the energy from the 202.5-Hz sine wave falls directly in a DFT bin. Using zero padding out to 2000 samples, obtain the DFT and plot the amplitude estimates.

```xdft = fft(x,2000);
xdft = xdft(1:length(xdft)/2+1);
xdft = 1/length(x).*xdft;
xdft(2:end-1) = 2*xdft(2:end-1);
freq = 0:Fs/(2*length(x)):Fs/2;
plot(freq,abs(xdft));
xlabel('Hz'); ylabel('Amplitude');
h = line(freq,ones(2*length(x)/2+1,1));
set(h,'color',[1 0 0],'linewidth',2);```

The use of zero padding enables you to estimate the amplitudes of both frequencies correctly.