In: Electrical Engineering
please let me know reference of this MATLAB code.
please explain this code line by line.
.
.
N=256; %% sampling number
n=linspace(0,1,N);
fs=1/(n(2)-n(1));
x=5*(sin((2*pi*10*n))); %% create signal
N=length(x);
f=[-fs/2:fs/N:fs/2-fs/N];
subplot(211)
plot(f,fftshift(abs(fft(x))));
title('|G(f)|');grid;
xlabel('frequency');
ylabel('|G(f)|');
%Zero padding
xx=[zeros(1,128) x zeros(1,128)];
N=length(xx);
f=[-fs/2:fs/N:fs/2-fs/N];
subplot(212)
plot(f,fftshift(abs(fft(xx))));
title('|Gz(f)|');grid;
xlabel('frequency');
ylabel('|Gz(f)|');
This code is see the effect of padding zeros in the signal. It increases the Resolution of the fft.
I have added comments to each line of code for your understanding
N=256; %The number of samples taken for x(n)
n=linspace(0,1,N); %creating the values of points @ where to sample the function x(n)
fs=1/(n(2)-n(1)); %sampling rate
x=5*(sin((2*pi*10*n))); %% create signal
N=length(x); %length of the signal is equal to
the number of samples taken
f=[-fs/2:fs/N:fs/2-fs/N]; %as we sample a signal with fs, We can only see the spectrum of it in the range -fs/2 to fs/2
subplot(211) %making a plot of 2 images in one and usign the first image
plot(f,fftshift(abs(fft(x)))); %ploting the fft of the signal
title('|G(f)|');grid;
xlabel('frequency');
ylabel('|G(f)|');
%Zero padding
xx=[zeros(1,128) x zeros(1,128)]; %padding zeros at the front and back of the signal
N=length(xx); %defining new lenghth of the signal
f=[-fs/2:fs/N:fs/2-fs/N]; %%as we sample a signal with fs, We can only see the spectrum of it in the range -fs/2 to fs/2
subplot(212) %making a plot of 2 images in one and usign the second image
plot(f,fftshift(abs(fft(xx)))); %ploting the fft of the padded
signal
title('|Gz(f)|');grid;
xlabel('frequency');
ylabel('|Gz(f)|');
As can be seen, plot 2 has more details on the graph than plot 1. This shows the increases resolution of the fft