How can I increase the zero-padding on the DFT?

Now I'm trying to understand and implement the zero-padding work in my example. As I know, meaningless zero values are affect to better DFT result. So I want to see how zero-padding works.

clear all
clc

fs = 50; %50Hz
T = 1/fs;
N = 8;
t = (0:N-1)*T;

y = 5+cos(2*pi*12.5*t)+sin(2*pi*18.75*t);

X=zeros(1,N);

for k = 0:N-1
for n = 0:N-1
X(k+1) = X(k+1) + y(n+1)*exp(-j*(2*pi/N)*k*n);
end
end

x_mag = abs(X);

f_2 = fs*(0:(N-1))/N;
stem(f_2,x_mag,'-ok','linewidth',2,'MarkerFaceColor','black');


For N=8:

For N=80:

But I feel hard to implement the increasing zero padding in my example code. Would you please show me how I can increase the zero-pad?

I found the better result when I increased the $N$ number to 80 from 8. But I'm not sure if increasing $N$ means increasing the zero-pad.

Your code does not implement the zero padding method. Below is the corrected version that does. Note that zero padding does not improve frequency resolution. It mereley provides interpolated samples of the original spectrum.

   clear all
clc

fs = 50; %50Hz
T = 1/fs;
N = 8;
t = (0:N-1)*T;

y = 5+cos(2*pi*12.5*t)+sin(2*pi*18.75*t);

M = 8;
K = M*N;       % DFT increased size
yk = [y zeros(1,(M-1)*N) ];  %zero padded input

X=zeros(1,K);   % high density

for k = 0:K-1
for n = 0:K-1
X(k+1) = X(k+1) + yk(n+1)*exp(-j*(2*pi/K)*k*n);
end
end

x_mag = abs(X);

f_2 = fs*(0:(K-1))/K;
stem(f_2,x_mag,'-ok','linewidth',2,'MarkerFaceColor','black');

• Thanks for letting me know that. From it, I can understand how the zero padding works. – start01 Nov 15 '17 at 8:15

You are right by suspecting that this is not what adding a zero pad means. What you did in your example just increased the resolution of your signal, which of course leads to a better resolution in the Fourier space.

To add a zero padding you should generate a zero vector of length M>N and paste your signal of N length in the middle of this vector. This will create a zero padding on both sides of your signal of length (M-N)/2 (work out the parity issues as a homework).