You are on page 1of 35

2012

DIGITAL COMMUNICATION
MATLAB ASSIGNMENT
SUBJECT CODE : EC 601

Sampling and Reconstruction

BHUVANESH V -1MS09EC021 L R VINOD 1MS09EC047 ADITYA NAYAK-1MS09EC007 DEEPAK V K-1MS09EC025 AJIT KRISHNA -1MS09EC008

Q) WRITE A MATLAB CODE TO SAMPLE A GIVEN ANALOG SIGNAL x(t)=A*cos(2*pi*f*t) AND TO OBTAIN A RECONSTRUCTED WAVEFORM USING LOW PASS FILTER . MEASURE THE DIFFERENCE B/W THE ORIGINAL AND RECONSTRUCTED SIGNAL . CONSIDER BOTH THE CASES WITH AND WITHOUT ALIASING EFFECT .
Algorithm for sampling: 1) Obtain analog signal. 2) Enter the max frequency (FM) and sampling frequency (FS). 3) Set sampling frequency (FS) either 2 times greater than or less than or equal to max frequency FM and sample analog signal.

The frequency domain representation of the signal is then displayed. We can observe the anti-aliasing or aliasing effect. Algorithm for reconstruction: 1) Magnitude of each sample of the signal is individually multiplied to a SINC function. 2) The SINC function of each sample is added to the reconstructed signal at its respected position in time. If the signal has been sampled properly we observe the reconstructed signal being the same as original, else aliasing is observed. For the ease of programming time axis has been normalized with respect to sampling period.

MATLAB CODE CASE 1) : WITHOUT ALIASING EFFECT [ FS>2*FM] %creating "analog" signal %clears all variables clc clear all close all t=0:.1:20; N=21; FM=400; %maximum frequency of the i/p signal

FS=2000; x=0.5*cos(2*pi*(FM/FS)*t); figure(1); subplot(2,1,1); plot(t,x); title('Original signal') xlabel('t'); ylabel('x(t)'); subplot(2,1,2); x_samples=x(1:10:201); stem(x_samples,'filled'); title('Sampled signal') xlabel('n'); ylabel('x(n)'); axis([0 20 -0.5 0.5]);

%sampling frequency

%plot of original signal

%gets 21 samples of x. %stem of sampled sequence

%i/p and sampled signal in freq domain figure(2); X=fft(x); Xmag=abs(X); fhz=t.*(FS/N); subplot(2,1,1); plot(fhz(1:N/2),Xmag(1:N/2));%plot of original seq in freq domain xlabel('frequency(Hz)'); ylabel('Amplitude'); %discrete fourier transform of i/p sequence %absolute value of original sequence %converting time domain to freq domain

title('i/p signal in frequency domain'); grid; X_samples=fft(x_samples); sequence X_mag=abs(X_samples); fhz=t.*(FS/N); subplot(2,1,2); plot(fhz(1:N/2),X_mag(1:N/2));%plot of sampled seq in freq domain xlabel('frequency(Hz)'); ylabel('Amplitude'); title('sampled signal in frequency domain'); grid; %creating dialog box with explanations l1=[blanks(10),'Sample by sample reconstruction.']; l2='Blue dots: Input samples.'; l3='Blue curve: reconstructed signal.'; l4='Red curve: contribution to output sample from current sample.'; l5='Press any key to update with 1 iteration.'; l6='(You can keep this window open while watching the reconstruction)'; information ={l1,'',l2,l3,l4,'',l5,'',l6}; %starting reconstruction process figure(3); messagebox=msgbox(information,'Information','help'); %absolute value of sampled sequence %converting time domain to freq domain %discrete fourier transform of sampled

subplot(2,1,2); plot(t,x,'black'); hold on; plot([0 20],[0 0],'black'); hold off; xlabel('t'); ylabel('x(t)'); title('Original signal'); grid; x_recon=0; subplot(2,1,1); for k=0:length(x_samples)-1 stem(0:length(x_samples)-1,x_samples,'filled'); %plotting discrete seq %of sample by sample reconstruction if k==length(x_samples)-1 title('Reconstruction finished'); else title('Sample by sample reconstruction'); end grid on; l=k:-.1:-20+k; x_recon=x_recon+x_samples(k+1)*sinc(l); axis([0 20 -0.5 0.5]); hold; plot(t,x_samples(k+1)*sinc(l),'r') %initializing x_recon %plot of original signal

plot(t,x_recon); hold off; waitforbuttonpress; end

%plotting the reconstruction seq

%Reconstructed signal in freq domain figure(4); X_recon=fft(x_recon); sequence X_Mag=abs(X_recon); fhz=t.*(FS/N); subplot(2,1,1); plot(fhz(1:N/2),X_Mag(1:N/2)); %plot of reconstructed seq in freq domain xlabel('frequency(Hz)'); ylabel('Amplitude'); title('reconstructed signal in frequency domain'); grid; %i/p signal in freq domain X=fft(x); Xmag=abs(X); fhz=t.*(FS/N); subplot(2,1,2); plot(fhz(1:N/2),Xmag(1:N/2)); xlabel('frequency(Hz)'); ylabel('Amplitude'); title('i/p signal in frequency domain'); grid; %plot of original seq in freq domain %discrete fourier transform of i/p sequence %absolute value of original sequence %converting time domain to freq domain %absolute value of reconstructed sequence %converting time domain to freq domain %discrete fourier transform of reconstructed

OUTPUT : N =21 FM = 400 FS = 2000


FIGURE (1)
Original signal 0.5

x(t)

-0.5

10 12 t Sampled signal

14

16

18

20

0.5

x(n)

-0.5

10 n

12

14

16

18

20

FIGURE(2)
i/p signal in frequency domain 60

Amplitude

40

20

10

20

40 50 60 70 frequency(Hz) sampled signal in frequency domain

30

80

90

Amplitude

10

20

30

40 50 frequency(Hz)

60

70

80

90

MESSAGE BOX

FIGURE (3)

(SAMPLE BY SAMPLE RECONSTRUCTION)

>AT FIRST SAMPLE


Sample by sample reconstruction 0.5

-0.5

10

12

14

16

18

20

Original signal 0.5

x(t)

-0.5

10 t

12

14

16

18

20

>AT FIFTH SAMPLE


Sample by sample reconstruction 0.5

-0.5

10

12

14

16

18

20

Original signal 0.5

x(t)

-0.5

10 t

12

14

16

18

20

>AT EIGHTH SAMPLE


Sample by sample reconstruction 0.5

-0.5

10

12

14

16

18

20

Original signal 0.5

x(t)

-0.5

10 t

12

14

16

18

20

>AT ELEVENTH SAMPLE

Sample by sample reconstruction 0.5

-0.5

10

12

14

16

18

20

Original signal 0.5

x(t)

-0.5

10 t

12

14

16

18

20

>AT FIFTEENTH SAMPLE


Sample by sample reconstruction 0.5

-0.5

10

12

14

16

18

20

Original signal 0.5

x(t)

-0.5

10 t

12

14

16

18

20

>AT EIGHTEENTH SAMPLE

Sample by sample reconstruction 0.5

-0.5

10

12

14

16

18

20

Original signal 0.5

x(t)

-0.5

10 t

12

14

16

18

20

>AT 20th SAMPLE


Reconstruction finished 0.5

-0.5

10

12

14

16

18

20

Original signal 0.5

x(t)

-0.5

10 t

12

14

16

18

20

FIGURE (4)

reconstructed signal in frequency domain 60

Amplitude

40

20

10

20

40 50 60 frequency(Hz) i/p signal in frequency domain

30

70

80

90

60

Amplitude

40

20

10

20

30

40 50 frequency(Hz)

60

70

80

90

*From the above graph we can observe that the reconstructed signal obtained in frequency domain is
same as that of the i/p signal in frequency domain . Hence the amplitude and frequencies are perfectly matching in both the plots . The difference between the reconstructed signal and the original signal is almost zero .

CASE 2) : WITHALIASING EFFECT [ FS>2*FM] %creating "analog" signal %clears all variables clc clear all close all t=0:.1:20; N=21; FM=400; FS=700; x=0.5*cos(2*pi*(FM/FS)*t); figure(1); subplot(2,1,1); plot(t,x); title('Original signal') xlabel('t'); ylabel('x(t)'); subplot(2,1,2); x_samples=x(1:10:201); %gets 21 samples of x. %plot of original signal %maximum frequency of the i/p signal %sampling frequency

stem(x_samples,'filled'); title('Sampled signal') xlabel('n'); ylabel('x(n)'); axis([0 20 -0.5 0.5]);

%stem of sampled sequence

%i/p and sampled signal in freq domain figure(2); X=fft(x); Xmag=abs(X); fhz=t.*(FS/N); subplot(2,1,1); plot(fhz(1:N/2),Xmag(1:N/2));%plot of original seq in freq domain xlabel('frequency(Hz)'); ylabel('Amplitude'); title('i/p signal in frequency domain'); grid; X_samples=fft(x_samples); sequence X_mag=abs(X_samples); fhz=t.*(FS/N); subplot(2,1,2); plot(fhz(1:N/2),X_mag(1:N/2));%plot of sampled seq in freq domain xlabel('frequency(Hz)'); ylabel('Amplitude'); %absolute value of sampled sequence %converting time domain to freq domain %discrete fourier transform of sampled %discrete fourier transform of i/p sequence %absolute value of original sequence %converting time domain to freq domain

title('sampled signal in frequency domain'); grid; %creating dialog box with explanations l1=[blanks(10),'Sample by sample reconstruction.']; l2='Blue dots: Input samples.'; l3='Blue curve: reconstructed signal.'; l4='Red curve: contribution to output sample from current sample.'; l5='Press any key to update with 1 iteration.'; l6='(You can keep this window open while watching the reconstruction)'; information ={l1,'',l2,l3,l4,'',l5,'',l6}; %starting reconstruction process figure(3); messagebox=msgbox(information,'Information','help'); subplot(2,1,2); plot(t,x,'black'); hold on; plot([0 20],[0 0],'black'); hold off; xlabel('t'); ylabel('x(t)'); title('Original signal'); grid; x_recon=0; %initializing x_recon %plot of original signal

subplot(2,1,1); for k=0:length(x_samples)-1 stem(0:length(x_samples)-1,x_samples,'filled'); %plotting discrete seq %of sample by sample reconstruction if k==length(x_samples)-1 title('Reconstruction finished'); else title('Sample by sample reconstruction'); end grid on; l=k:-.1:-20+k; x_recon=x_recon+x_samples(k+1)*sinc(l); axis([0 20 -0.5 0.5]); hold; plot(t,x_samples(k+1)*sinc(l),'r') plot(t,x_recon); hold off; waitforbuttonpress; end %Reconstructed signal in freq domain figure(4); X_recon=fft(x_recon); sequence X_Mag=abs(X_recon); fhz=t.*(FS/N); subplot(2,1,1); %absolute value of reconstructed sequence %converting time domain to freq domain %discrete fourier transform of reconstructed %plotting the reconstruction seq

plot(fhz(1:N/2),X_Mag(1:N/2)); %plot of reconstructed seq in freq domain xlabel('frequency(Hz)'); ylabel('Amplitude'); title('reconstructed signal in frequency domain'); grid; %i/p signal in freq domain

X=fft(x); Xmag=abs(X); fhz=t.*(FS/N); subplot(2,1,2); plot(fhz(1:N/2),Xmag(1:N/2)); xlabel('frequency(Hz)'); ylabel('Amplitude');

%discrete fourier transform of i/p sequence %absolute value of original sequence %converting time domain to freq domain %plot of original seq in freq domain

title('i/p signal in frequency domain'); grid;

OUTPUT : N =21 FM = 400 FS = 700

FIGURE (1)
Original signal 0.5

x(t)

-0.5

10 12 t Sampled signal

14

16

18

20

0.5

x(n)

-0.5

10 n

12

14

16

18

20

FIGURE(2)
i/p signal in frequency domain 6

Amplitude

15 20 25 frequency(Hz) sampled signal in frequency domain

10

30

35

Amplitude

10

15 20 frequency(Hz)

25

30

35

MESSAGE BOX

FIGURE (3)

(SAMPLE BY SAMPLE RECONSTRUCTION)

>AT FIRST SAMPLE

Sample by sample reconstruction 0.5

-0.5

10

12

14

16

18

20

Original signal 0.5

x(t)

-0.5

10 t

12

14

16

18

20

>AT FIFTH SAMPLE


Sample by sample reconstruction 0.5

-0.5

10

12

14

16

18

20

Original signal 0.5

x(t)

-0.5

10 t

12

14

16

18

20

>AT EIGHTH SAMPLE


Sample by sample reconstruction 0.5

-0.5

10

12

14

16

18

20

Original signal 0.5

x(t)

-0.5

10 t

12

14

16

18

20

>AT ELEVENTH SAMPLE


Sample by sample reconstruction 0.5

-0.5

10

12

14

16

18

20

Original signal 0.5

x(t)

-0.5

10 t

12

14

16

18

20

>AT FIFTEENTH SAMPLE


Sample by sample reconstruction 0.5

-0.5

10

12

14

16

18

20

Original signal 0.5

x(t)

-0.5

10 t

12

14

16

18

20

>AT EIGHTEENTH SAMPLE

Sample by sample reconstruction 0.5

-0.5

10

12

14

16

18

20

Original signal 0.5

x(t)

-0.5

10 t

12

14

16

18

20

>AT 20th SAMPLE


Reconstruction finished 0.5

-0.5

10

12

14

16

18

20

Original signal 0.5

x(t)

-0.5

10 t

12

14

16

18

20

FIGURE (4)

reconstructed signal in frequency domain 40 30 20 10 0

Amplitude

10

15 20 25 frequency(Hz) i/p signal in frequency domain

30

35

Amplitude

10

15 20 frequency(Hz)

25

30

35

*From the above graph we can observe that the reconstructed signal obtained in frequency domain is
not same as that of the i/p signal in frequency domain . Hence the amplitude and frequencies are not perfectly matching in both the plots . The difference between the reconstructed signal and the original signal is : 1) At frequency of 10 hz : amplitude of original signal is 1.009 and amplitude of reconstructed signal is 1.644 . Therefore the difference is of .645 2) At frequency of 20 hz : amplitude of original signal is 2.121 and amplitude of reconstructed signal is 5.26 . Therefore the difference is of 3.139 3) At frequency of 30 hz : amplitude of original signal is 5.725 and amplitude of reconstructed signal is 37.93 . Therefore the difference is of 32.205

CASE 3) : FS=2*FM %creating "analog" signal %clears all variables clc clear all close all t=0:.1:20; N=21; FM=400; FS=800; x=0.5*cos(2*pi*(FM/FS)*t); figure(1); subplot(2,1,1); plot(t,x); title('Original signal') xlabel('t'); ylabel('x(t)'); subplot(2,1,2); x_samples=x(1:10:201); stem(x_samples,'filled'); title('Sampled signal') xlabel('n'); ylabel('x(n)'); axis([0 20 -0.5 0.5]); %gets 21 samples of x. %stem of sampled sequence %plot of original signal %maximum frequency of the i/p signal %sampling frequency

%i/p and sampled signal in freq domain figure(2); X=fft(x); Xmag=abs(X); fhz=t.*(FS/N); subplot(2,1,1); plot(fhz(1:N/2),Xmag(1:N/2));%plot of original seq in freq domain xlabel('frequency(Hz)'); ylabel('Amplitude'); title('i/p signal in frequency domain'); grid; X_samples=fft(x_samples); sequence X_mag=abs(X_samples); fhz=t.*(FS/N); subplot(2,1,2); plot(fhz(1:N/2),X_mag(1:N/2));%plot of sampled seq in freq domain xlabel('frequency(Hz)'); ylabel('Amplitude'); title('sampled signal in frequency domain'); grid; %creating dialog box with explanations l1=[blanks(10),'Sample by sample reconstruction.']; l2='Blue dots: Input samples.'; %absolute value of sampled sequence %converting time domain to freq domain %discrete fourier transform of sampled %discrete fourier transform of i/p sequence %absolute value of original sequence %converting time domain to freq domain

l3='Blue curve: reconstructed signal.'; l4='Red curve: contribution to output sample from current sample.'; l5='Press any key to update with 1 iteration.'; l6='(You can keep this window open while watching the reconstruction)'; information ={l1,'',l2,l3,l4,'',l5,'',l6}; %starting reconstruction process figure(3); messagebox=msgbox(information,'Information','help'); subplot(2,1,2); plot(t,x,'black'); hold on; plot([0 20],[0 0],'black'); hold off; xlabel('t'); ylabel('x(t)'); title('Original signal'); grid; x_recon=0; subplot(2,1,1); for k=0:length(x_samples)-1 stem(0:length(x_samples)-1,x_samples,'filled'); %plotting discrete seq %of sample by sample reconstruction if k==length(x_samples)-1 title('Reconstruction finished'); %initializing x_recon %plot of original signal

else title('Sample by sample reconstruction'); end grid on; l=k:-.1:-20+k; x_recon=x_recon+x_samples(k+1)*sinc(l); axis([0 20 -0.5 0.5]); hold; plot(t,x_samples(k+1)*sinc(l),'r') plot(t,x_recon); hold off; waitforbuttonpress; end %Reconstructed signal in freq domain figure(4); X_recon=fft(x_recon); sequence X_Mag=abs(X_recon); fhz=t.*(FS/N); subplot(2,1,1); plot(fhz(1:N/2),X_Mag(1:N/2)); %plot of reconstructed seq in freq domain xlabel('frequency(Hz)'); ylabel('Amplitude'); title('reconstructed signal in frequency domain'); grid; %i/p signal in freq domain %absolute value of reconstructed sequence %converting time domain to freq domain %discrete fourier transform of reconstructed %plotting the reconstruction seq

X=fft(x); Xmag=abs(X); fhz=t.*(FS/N); subplot(2,1,2); plot(fhz(1:N/2),Xmag(1:N/2)); xlabel('frequency(Hz)'); ylabel('Amplitude');

%discrete fourier transform of i/p sequence %absolute value of original sequence %converting time domain to freq domain %plot of original seq in freq domain

title('i/p signal in frequency domain'); grid;

OUTPUT : N =21 FM = 400 FS = 800

FIGURE (1)
Original signal 0.5

x(t)

-0.5

10 12 t Sampled signal

14

16

18

20

0.5

x(n)

-0.5

10 n

12

14

16

18

20

FIGURE(2)
i/p signal in frequency domain 3

Amplitude

15 20 25 frequency(Hz) sampled signal in frequency domain

10

30

35

2.5 2 1.5 1 0.5

Amplitude

10

15 20 frequency(Hz)

25

30

35

MESSAGE BOX

FIGURE (3)

(SAMPLE BY SAMPLE RECONSTRUCTION)

>AT FIRST SAMPLE


Sample by sample reconstruction 0.5

-0.5

10

12

14

16

18

20

Original signal 0.5

x(t)

-0.5

10 t

12

14

16

18

20

>AT FIFTH SAMPLE


Sample by sample reconstruction 0.5

-0.5

10

12

14

16

18

20

Original signal 0.5

x(t)

-0.5

10 t

12

14

16

18

20

>AT EIGHTH SAMPLE


Sample by sample reconstruction 0.5

-0.5

10

12

14

16

18

20

Original signal 0.5

x(t)

-0.5

10 t

12

14

16

18

20

>AT ELEVENTH SAMPLE


Sample by sample reconstruction 0.5

-0.5

10

12

14

16

18

20

Original signal 0.5

x(t)

-0.5

10 t

12

14

16

18

20

>AT FIFTEENTH SAMPLE


Sample by sample reconstruction 0.5

-0.5

10

12

14

16

18

20

Original signal 0.5

x(t)

-0.5

10 t

12

14

16

18

20

>AT EIGHTEENTH SAMPLE


Sample by sample reconstruction 0.5

-0.5

10

12

14

16

18

20

Original signal 0.5

x(t)

-0.5

10 t

12

14

16

18

20

>AT 20th SAMPLE

Reconstruction finished 0.5

-0.5

10

12

14

16

18

20

Original signal 0.5

x(t)

-0.5

10 t

12

14

16

18

20

FIGURE (4)

reconstructed signal in frequency domain 10 8 6 4 2

Amplitude

10

15 20 25 frequency(Hz) i/p signal in frequency domain

30

35

Amplitude

10

15 20 frequency(Hz)

25

30

35

*From the above graph we can observe that the reconstructed signal obtained in frequency domain is
not exactly same as that of the i/p signal in frequency domain . Hence the amplitude and frequencies are not perfectly matching in both the plots . The difference between the reconstructed signal and the original signal is : 1) At frequency of 10 hz : amplitude of original signal is 0.5397 and amplitude of reconstructed signal is 3.09. Therefore the difference is of 2.5503 2) At frequency of 20 hz : amplitude of original signal is 0.6718 and amplitude of reconstructed signal is3.713. Therefore the difference is of 3.0412 3) At frequency of 30 hz : amplitude of original signal is 1.261 and amplitude of reconstructed signal is 5.996 . Therefore the difference is of 4.735

You might also like