MATLAB Code for Sampling Theorem and effect of under sampling

Shubham Gupta

--

%Sampling and recovery of signal

df=0.1;

T=10*10^(-3); %Sampling Period

Ts=1/10000;

fs=1/Ts;

t=0:Ts:10;

N=size(t);

s=zeros(N);

s(1:50)=ones(size(s(1:50)));

figure(1);

subplot(2,2,1);

plot(t,s);

xlabel(‘Time’);

ylabel(‘Amplitude’);

title(‘Rectangle Pulse ‘);

axis([0 0.015 0 1.2]);

Y=fft(s);

P2 = abs(Y/fs);%Two Sided Spectrum

subplot(2,2,2);

f1=fftshift(P2);

f=-length(f1)/2:1:length(f1–1)/2–1;

plot(f,f1);

ylabel(‘Amplitude ‘);

title(‘Fourier Spectrum of Rect. Pulse ‘);

grid on

msg=sin(2*pi*5*t)+sin(2*pi*15*t);

subplot(2,2,3);

plot(t,msg);

xlabel(‘Time’);

ylabel(‘Amplitude’);

title(‘Message Pulse ‘);

axis([0 0.11 -0.2 2]);

Y=fft(msg);

P2 = abs(Y/fs);%Two Sided Spectrum

subplot(2,2,4);

f1=fftshift(P2);

f=-length(f1)/2:1:length(f1–1)/2–1;

plot(f,f1);

ylabel(‘Amplitude ‘);

axis([-200 200 -0.2 6]);

title(‘Fourier Spectrum of Msg. Signal ‘);

grid on

figure(2);

pul=square(2*pi*300*t)/2 + 0.5;

subplot(2,2,1);

plot(t,pul);

title(‘Sampling Pulse ‘);

axis([0 0.15 -0.1 1.2]);

smp=pul.*msg;

subplot(2,2,2);

plot(t,smp);

title(‘Sampled Signal ‘);

axis([0 0.1 -0.2 2]);

Y=fft(smp);

P2 = abs(Y/fs);%Two Sided Spectrum

subplot(2,2,3);

f1=fftshift(P2);

f=-length(f1)/2:1:length(f1–1)/2–1;

plot(f,f1);

ylabel(‘Amplitude ‘);

title(‘Fourier Spectrum of Sampled Signal ‘);

grid on

lp=smp;

lp=lowpass(smp,30,50);

subplot(2,2,4);

plot(t,lp);

grid on;

xlabel(‘Time’);

ylabel(‘Amplitude’);

title(‘Low pass filter output ‘);

axis([0 0.1 -0.2 2]);

N=length(smp);

for i=1:N-1

if smp(i)<=0

smp(i)=0;

end

end

n=1;

a(1)=smp(1);

for i=15:60:N-1;

n=n+1;

a(n)=max(lp(i-14:i+15));

end

figure(3);

subplot(2,2,1);

plot(t(1:length(a)),a);

xlabel(‘Time’);

ylabel(‘Amplitude’);

title(‘Recovered Signal ‘);

axis([0 0.1 -0.2 2]);

Y=fft(a);

P2 = abs(Y/fs);%Two Sided Spectrum

subplot(2,2,2);

f1=fftshift(P2);

f=-length(f1)/2:1:length(f1–1)/2–1;

plot(f,f1);

ylabel(‘Amplitude ‘);

axis([-200 200 -0.2 6]);

title(‘Fourier Spectrum of Rcovered Signal ‘);

Thank me later.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Shubham Gupta
Shubham Gupta

Written by Shubham Gupta

Software Engineer and Content Writer at Lovebird Lingerie

No responses yet

Write a response