# Sampling Theorem MATLAB Verification

**Logic used in Sampling Theorem MATLAB**

**To get Sampling figure1**

- Generate analog signal. Plot this analog signal into the figure1.
- Generate undersampled signal. Plot this into the figure1.
- Generate critical signal. Plot this into the figure1.
- Generate oversampled signal. Plot this into the figure1.

**To get Reconstruction figure2**

- Plot the analog signal into the figure2.
- Interpolate the generated undersampled signal. Plot this into the figure2.
- Interpolate the generated critical signal. Plot this into the figure2.
- Interpolate the generated oversampled signal. Plot this into the figure2.

**MATLAB Program to verify Sampling Theorem**

close all; clear; %1constreuct analog signal tfinal=0.01; F=input('enter the analog frequency:'); t=0:0.00001:tfinal; xt=cos(2*pi*F*t); %plot the analog signal subplot(4,1,1); plot(t,xt); title('analog signal'); xlabel('time'); ylabel('amplitude'); %under sampling Fs1=1.3*F; n1=0:1/Fs1:tfinal; x1n=cos(2*pi*n1*F); subplot(4,1,2); stem(n1,x1n); title('under sampled signal'); xlabel('time'); ylabel('amplitude'); %critical sampling Fs2=2*F; n2=0:1/Fs2:tfinal; x2n=cos(2*pi*n2*F); subplot(4,1,3); stem(n2,x2n); title('critical sampled signal'); xlabel('time'); ylabel('amplitude'); %over sampling Fs3=10*F; n3=0:1/Fs3:tfinal; x3n=cos(2*pi*n3*F); subplot(4,1,4); stem(n3,x3n); title('over sampled signal'); xlabel('time'); ylabel('amplitude'); figure subplot(4,1,1); plot(t,xt); title('analog signal'); xlabel('time'); ylabel('amplitude'); % xr1=interp(x1n,2); nr1=interp(n1,2); subplot(4,1,2); plot(nr1,xr1); title('reconstruted from under sampled signals'); xlabel('time'); ylabel('amplitude'); xr2=interp(x2n,2); nr2=interp(n2,2); subplot(4,1,3); plot(nr2,xr2); title('reconstruted from right sampled signals'); xlabel('time'); ylabel('amplitude'); xr3=interp(x3n,2); nr3=interp(n3,2); subplot(4,1,4); plot(nr3,xr3); title('reconstruted from over sampled signals'); xlabel('time'); ylabel('amplitude');

**Basics to understand the Sampling Theorem MATLAB**

subplot(m,n,p)

- Divides figure into m rows and n columns, p represents the axes specified for that subplot.
- The first subplot is the first column of the first row, the second subplot is the second column of the first row, and so on.
- In this case, subplot(4,1,1)- In entire figure, 4 rows, 1 column ,and its position is 1.

plot(x,y)

- 2D line plot of data y v/s x.( plot is used only for analog signal).

title, xlabel, ylabel

- title represents the title of a subplot.
- xlabel represents the X-axis name.
- ylabel represents y-axis name.

stem(n1, x1n)

- It plots data sequences of n1 at a specified value of x1n.
- stem is used only for a discrete sequence.

figure

- It is used to get the second sequence.

##### Matlab Predefined Functions used in Sampling Theorem MATLAB

isys=interp(sys,fre) ;

- interpolates frequency response data contain FRD model system at frequency freqs.
- xr1=interp(x1n,2) // It increases the sampled rate of sampled signal x1n by 2times. This increased sampled rate signal is transferred to xr1.
- nr1=interp(n1,2) // It increases the time vector of sampled signal nr1 by 2 times.

###### Explanation of Each Step involved in Sampling Theorem MATLAB

1) Generate analog signal. Plot this analog signal into the figure1.

close all; clear; %1construct analog signal tfinal=0.01; F=input('enter the analog frequency:'); t=0:0.00001:tfinal; xt=cos(2*pi*F*t); %plot the analog signal subplot(4,1,1); plot(t,xt); title('analog signal'); xlabel('time'); ylabel('amplitude');

**Construct analog signal**

- We need to generate analog signal of xt= cos (2*pi*fd*t) , where , t= time base vector=0:samples steps : tfinal.
- t= 0:0.00001:tfinal; means time vector running from o to o.01(t final) in steps of 0.00001.
- fd= desired frequency( input value).
- t= time base vector=0:samples steps : tfinal.
- tfinal - a length of the analog signal.

**2.Plot the analog signal**

- We need to plot the analog signal in a figure.
- Subplot(m,n,p)--divides the figure into m rows and n columns, p represents the axes specified for that subplot.
- In this case subplot(4,1,1)- In the entire figure, 4 rows, 1 column, and its position is 1.
- The first subplot is the first column of the first row, the second subplot is the second column of the first row, and so on.
- So, 4 rows, 1 column, then its position 1st.

2) Generate under-sampled signal. Plot this into the figure1.

%under sampling Fs1=1.3*F; n1=0:1/Fs1:tfinal; x1n=cos(2*pi*n1*F); subplot(4,1,2); stem(n1,x1n); title('under sampled signal'); xlabel('time'); ylabel('amplitude');

- Firstly, we have to generate the undersampled signal(x1n) of the sampling frequency(Fs1), the time vector(n1).
- As we know for undersampled signal, fs<=2fm (sampling frequency is less than Nyquist rate).
- FM- message signal or the signal frequency we applied(F).
- Aliasing happens in undersampling, where the overlap of the higher frequency component in terms of the continuous-time signal.
- So,we are taking Fs1 as 1.3*F because it is less than 2F( under sampling).
- n*(time vector) = 0: T* : tfinal// T*=1/fs=sampling rate.
- In this case, time vector(n1)= 0: 1/Fs1 : 0.01; // It means time vector running from 0 to o.1(tfinal) having sampling rate T*.
- x1n = under sampled signal= cos(2*pi*F*time vector(n*)) = cos(2*pi*F*n1) // In this case n1 is time vector.
- subplot (4,1,2) - Figure of 4 rows and 1 column, axis position is 2.

3) Generate critical signal. Plot this into the figure1.

%critical sampling Fs2=2*F; n2=0:1/Fs2:tfinal; x2n=cos(2*pi*n2*F); subplot(4,1,3); stem(n2,x2n); title('critical sampled signal'); xlabel('time'); ylabel('amplitude');

- Secondly, we have to generate a critical sampled signal(x2n) of the sampling frequency(fs2), time vector(n2).
- As we know for critical sampled signal, fs=2fm.
- So, we are taking fs2 as 2*F.
- n2 (time vector) = 0: 1/Fs2:tfinal;
- x2n = Critical sampled signal = cos(2*pi*F* n2);
- subplot (4,1,3) - Figure of 4 rows and 1 column, this subplot axis position is 3.
- stem(n2, x2n) - It plots data sequences of n2 with respect to a specified value of x2n.(stem is used only for data sequence).

4) Generate over-sampled signal. Plot this into the figure1.

%over sampling Fs3=10*F; n3=0:1/Fs3:tfinal; x3n=cos(2*pi*n3*F); subplot(4,1,4); stem(n3,x3n); title('over sampled signal'); xlabel('time'); ylabel('amplitude');

- Thirdly, we have to generate an over-sampled signal (x3n) of the sampling frequency (fs3), time vector(n3).
- For over-sampling, fs> 2fm so we are taking fs=10*F.
- subplot(4,1,4) - a figure of 4 rows, 1 column, this subplot axis position is 4.

5) Interpolate the generated under-sampled signal. Plot this into the figure2.

xr1=interp(x1n,2); nr1=interp(n1,2); subplot(4,1,2); plot(nr1,xr1); title('reconstruted from under sampled signals'); xlabel('time'); ylabel('amplitude');

- Now, We have to convert sampled signal to analog signal.
- In simple, we are increasing the sampled rate. So that dots in the sampled signal will join, we get an analog signal.
- xr1=interp(x1n,2) // It increases the sampled rate of sampled signal x1n by 2times. This increased sampled rate signal is transferred to xr1.
- nr1=interp(n1,2) // It increases the time vector of sampled signal nr1 by 2 times.
- Then we are plotting x-axis as xr1, y-axis as nr1.

3) Interpolate the generated critical signal, over-sampled signal. Plot this into the figure2.

xr2=interp(x2n,2); nr2=interp(n2,2); subplot(4,1,3); plot(nr2,xr2); title('reconstruted from right sampled signals'); xlabel('time'); ylabel('amplitude'); xr3=interp(x3n,2); nr3=interp(n3,2); subplot(4,1,4); plot(nr3,xr3); title('reconstruted from over sampled signals'); xlabel('time'); ylabel('amplitude');

**Output of sampling theorem Matlab**

**Related Blogs**

**VHDL Tutorials**

HDL Code To Simulate Full Adder

HDL Code To Simulate 1-Bit Comparator

HDL Code to Simulate 8:3 Priority Encoder

HDL Code To Simulate 4-bit Binary To Gray Converter

HDL Code To Simulate 1:4 demux

Its awesome

Thanks For Your Support Akshay Krishna.

Great work bro !!

Thank you.