35
Sampling and Reconstruction BHUVANESH V -1MS09EC021 L R VINOD – 1MS09EC047 DEEPAK V K-1MS09EC025 ADITYA NAYAK-1MS09EC007 AJIT KRISHNA -1MS09EC008

Dc Assignment

Embed Size (px)

Citation preview

Page 1: Dc Assignment

Sampling and Reconstruction

BHUVANESH V -1MS09EC021

L R VINOD – 1MS09EC047 DEEPAK V K-1MS09EC025

ADITYA NAYAK-1MS09EC007 AJIT KRISHNA -1MS09EC008

Page 2: Dc Assignment

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 variablesclcclear allclose allt=0:.1:20;N=21;FM=400; %maximum frequency of the i/p signal FS=2000; %sampling frequency x=0.5*cos(2*pi*(FM/FS)*t);figure(1);

Page 3: Dc Assignment

subplot(2,1,1);plot(t,x); %plot of original signaltitle('Original signal')xlabel('t');ylabel('x(t)'); subplot(2,1,2);x_samples=x(1:10:201); %gets 21 samples of x.stem(x_samples,'filled'); %stem of sampled sequencetitle('Sampled signal')xlabel('n');ylabel('x(n)');axis([0 20 -0.5 0.5]); %i/p and sampled signal in freq domainfigure(2);X=fft(x); %discrete fourier transform of i/p sequenceXmag=abs(X); %absolute value of original sequencefhz=t.*(FS/N); %converting time domain to freq domainsubplot(2,1,1);plot(fhz(1:N/2),Xmag(1:N/2));%plot of original seq in freq domainxlabel('frequency(Hz)');ylabel('Amplitude');title('i/p signal in frequency domain');grid; X_samples=fft(x_samples); %discrete fourier transform of sampled sequenceX_mag=abs(X_samples); %absolute value of sampled sequencefhz=t.*(FS/N); %converting time domain to freq domainsubplot(2,1,2);plot(fhz(1:N/2),X_mag(1:N/2));%plot of sampled seq in freq domainxlabel('frequency(Hz)');ylabel('Amplitude');

Page 4: Dc Assignment

title('sampled signal in frequency domain');grid; %creating dialog box with explanationsl1=[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 processfigure(3);messagebox=msgbox(information,'Information','help');subplot(2,1,2);plot(t,x,'black');hold on;plot([0 20],[0 0],'black'); %plot of original signalhold off;xlabel('t');ylabel('x(t)');title('Original signal');grid; x_recon=0; %initializing x_reconsubplot(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;

Page 5: Dc Assignment

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); %plotting the reconstruction seq hold off; waitforbuttonpress; end%Reconstructed signal in freq domainfigure(4);X_recon=fft(x_recon); %discrete fourier transform of reconstructed sequenceX_Mag=abs(X_recon); %absolute value of reconstructed sequencefhz=t.*(FS/N); %converting time domain to freq domainsubplot(2,1,1);plot(fhz(1:N/2),X_Mag(1:N/2)); %plot of reconstructed seq in freq domainxlabel('frequency(Hz)');ylabel('Amplitude');title('reconstructed signal in frequency domain');grid;%i/p signal in freq domainX=fft(x); %discrete fourier transform of i/p sequenceXmag=abs(X); %absolute value of original sequencefhz=t.*(FS/N); %converting time domain to freq domainsubplot(2,1,2);plot(fhz(1:N/2),Xmag(1:N/2)); %plot of original seq in freq domainxlabel('frequency(Hz)');ylabel('Amplitude');title('i/p signal in frequency domain');grid;

Page 6: Dc Assignment

OUTPUT :

N =21

FM = 400

FS = 2000

FIGURE (1)

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5Original signal

t

x(t)

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5Sampled signal

n

x(n)

Page 7: Dc Assignment

FIGURE(2)

0 10 20 30 40 50 60 70 80 900

20

40

60

frequency(Hz)

Am

plit

ude

i/p signal in frequency domain

0 10 20 30 40 50 60 70 80 900

2

4

6

frequency(Hz)

Am

plit

ude

sampled signal in frequency domain

MESSAGE BOX

FIGURE (3) (SAMPLE BY SAMPLE RECONSTRUCTION)

>AT FIRST SAMPLE

Page 8: Dc Assignment

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5

t

x(t

)

Original signal

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5Sample by sample reconstruction

>AT FIFTH SAMPLE

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5

t

x(t

)

Original signal

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5Sample by sample reconstruction

Page 9: Dc Assignment

>AT EIGHTH SAMPLE

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5

t

x(t)

Original signal

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5Sample by sample reconstruction

>AT ELEVENTH SAMPLE

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5

t

x(t

)

Original signal

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5Sample by sample reconstruction

Page 10: Dc Assignment

>AT FIFTEENTH SAMPLE

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5

t

x(t

)

Original signal

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5Sample by sample reconstruction

>AT EIGHTEENTH SAMPLE

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5

t

x(t

)

Original signal

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5Sample by sample reconstruction

>AT 20th SAMPLE

Page 11: Dc Assignment

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5

t

x(t

)

Original signal

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5Reconstruction finished

FIGURE (4)

0 10 20 30 40 50 60 70 80 900

20

40

60

frequency(Hz)

Am

plitu

de

reconstructed signal in frequency domain

0 10 20 30 40 50 60 70 80 900

20

40

60

frequency(Hz)

Am

plitu

de

i/p signal in frequency domain

Page 12: Dc Assignment

*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 variablesclcclear allclose allt=0:.1:20;N=21;FM=400; %maximum frequency of the i/p signal FS=700; %sampling frequency x=0.5*cos(2*pi*(FM/FS)*t);figure(1);subplot(2,1,1);plot(t,x); %plot of original signaltitle('Original signal')xlabel('t');ylabel('x(t)'); subplot(2,1,2);x_samples=x(1:10:201); %gets 21 samples of x.stem(x_samples,'filled'); %stem of sampled sequencetitle('Sampled signal')xlabel('n');ylabel('x(n)');axis([0 20 -0.5 0.5]); %i/p and sampled signal in freq domainfigure(2);X=fft(x); %discrete fourier transform of i/p sequence

Page 13: Dc Assignment

Xmag=abs(X); %absolute value of original sequencefhz=t.*(FS/N); %converting time domain to freq domainsubplot(2,1,1);plot(fhz(1:N/2),Xmag(1:N/2));%plot of original seq in freq domainxlabel('frequency(Hz)');ylabel('Amplitude');title('i/p signal in frequency domain');grid; X_samples=fft(x_samples); %discrete fourier transform of sampled sequenceX_mag=abs(X_samples); %absolute value of sampled sequencefhz=t.*(FS/N); %converting time domain to freq domainsubplot(2,1,2);plot(fhz(1:N/2),X_mag(1:N/2));%plot of sampled seq in freq domainxlabel('frequency(Hz)');ylabel('Amplitude');title('sampled signal in frequency domain');grid; %creating dialog box with explanationsl1=[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 processfigure(3);messagebox=msgbox(information,'Information','help');subplot(2,1,2);

Page 14: Dc Assignment

plot(t,x,'black');hold on;plot([0 20],[0 0],'black'); %plot of original signalhold off;xlabel('t');ylabel('x(t)');title('Original signal');grid; x_recon=0; %initializing x_reconsubplot(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); %plotting the reconstruction seq hold off; waitforbuttonpress; end%Reconstructed signal in freq domainfigure(4);X_recon=fft(x_recon); %discrete fourier transform of reconstructed sequenceX_Mag=abs(X_recon); %absolute value of reconstructed sequencefhz=t.*(FS/N); %converting time domain to freq domain

Page 15: Dc Assignment

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

X=fft(x); %discrete fourier transform of i/p sequenceXmag=abs(X); %absolute value of original sequencefhz=t.*(FS/N); %converting time domain to freq domainsubplot(2,1,2);plot(fhz(1:N/2),Xmag(1:N/2)); %plot of original seq in freq domainxlabel('frequency(Hz)');ylabel('Amplitude');title('i/p signal in frequency domain');grid;

OUTPUT :

N =21

FM = 400

FS = 700

Page 16: Dc Assignment

FIGURE (1)

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5Original signal

t

x(t

)

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5Sampled signal

n

x(n

)

Page 17: Dc Assignment

FIGURE(2)

0 5 10 15 20 25 30 350

2

4

6

frequency(Hz)

Am

plitu

dei/p signal in frequency domain

0 5 10 15 20 25 30 350

2

4

6

frequency(Hz)

Am

plitu

de

sampled signal in frequency domain

MESSAGE BOX

FIGURE (3) (SAMPLE BY SAMPLE RECONSTRUCTION)

>AT FIRST SAMPLE

Page 18: Dc Assignment

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5

t

x(t)

Original signal

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5Sample by sample reconstruction

>AT FIFTH SAMPLE

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5

t

x(t

)

Original signal

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5Sample by sample reconstruction

Page 19: Dc Assignment

>AT EIGHTH SAMPLE

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5

t

x(t)

Original signal

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5Sample by sample reconstruction

>AT ELEVENTH SAMPLE

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5

t

x(t

)

Original signal

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5Sample by sample reconstruction

Page 20: Dc Assignment

>AT FIFTEENTH SAMPLE

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5

t

x(t

)

Original signal

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5Sample by sample reconstruction

>AT EIGHTEENTH SAMPLE

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5

t

x(t

)

Original signal

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5Sample by sample reconstruction

>AT 20th SAMPLE

Page 21: Dc Assignment

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5

t

x(t

)

Original signal

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5Reconstruction finished

FIGURE (4)

0 5 10 15 20 25 30 350

10

20

30

40

frequency(Hz)

Am

plitu

de

reconstructed signal in frequency domain

0 5 10 15 20 25 30 350

2

4

6

frequency(Hz)

Am

plitu

de

i/p signal in frequency domain

Page 22: Dc Assignment

*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 variablesclcclear allclose allt=0:.1:20;N=21;FM=400; %maximum frequency of the i/p signal FS=800; %sampling frequency x=0.5*cos(2*pi*(FM/FS)*t);figure(1);subplot(2,1,1);plot(t,x); %plot of original signaltitle('Original signal')xlabel('t');ylabel('x(t)'); subplot(2,1,2);x_samples=x(1:10:201); %gets 21 samples of x.stem(x_samples,'filled'); %stem of sampled sequencetitle('Sampled signal')xlabel('n');ylabel('x(n)');

Page 23: Dc Assignment

axis([0 20 -0.5 0.5]); %i/p and sampled signal in freq domainfigure(2);X=fft(x); %discrete fourier transform of i/p sequenceXmag=abs(X); %absolute value of original sequencefhz=t.*(FS/N); %converting time domain to freq domainsubplot(2,1,1);plot(fhz(1:N/2),Xmag(1:N/2));%plot of original seq in freq domainxlabel('frequency(Hz)');ylabel('Amplitude');title('i/p signal in frequency domain');grid; X_samples=fft(x_samples); %discrete fourier transform of sampled sequenceX_mag=abs(X_samples); %absolute value of sampled sequencefhz=t.*(FS/N); %converting time domain to freq domainsubplot(2,1,2);plot(fhz(1:N/2),X_mag(1:N/2));%plot of sampled seq in freq domainxlabel('frequency(Hz)');ylabel('Amplitude');title('sampled signal in frequency domain');grid; %creating dialog box with explanationsl1=[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)';

Page 24: Dc Assignment

information ={l1,'',l2,l3,l4,'',l5,'',l6}; %starting reconstruction processfigure(3);messagebox=msgbox(information,'Information','help');subplot(2,1,2);plot(t,x,'black');hold on;plot([0 20],[0 0],'black'); %plot of original signalhold off;xlabel('t');ylabel('x(t)');title('Original signal');grid; x_recon=0; %initializing x_reconsubplot(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); %plotting the reconstruction seq hold off; waitforbuttonpress; end%Reconstructed signal in freq domainfigure(4);

Page 25: Dc Assignment

X_recon=fft(x_recon); %discrete fourier transform of reconstructed sequenceX_Mag=abs(X_recon); %absolute value of reconstructed sequencefhz=t.*(FS/N); %converting time domain to freq domainsubplot(2,1,1);plot(fhz(1:N/2),X_Mag(1:N/2)); %plot of reconstructed seq in freq domainxlabel('frequency(Hz)');ylabel('Amplitude');title('reconstructed signal in frequency domain');grid;%i/p signal in freq domain

X=fft(x); %discrete fourier transform of i/p sequenceXmag=abs(X); %absolute value of original sequencefhz=t.*(FS/N); %converting time domain to freq domainsubplot(2,1,2);plot(fhz(1:N/2),Xmag(1:N/2)); %plot of original seq in freq domainxlabel('frequency(Hz)');ylabel('Amplitude');title('i/p signal in frequency domain');grid;

OUTPUT :

N =21

FM = 400

FS = 800

Page 26: Dc Assignment

FIGURE (1)

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5Original signal

t

x(t

)

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5Sampled signal

n

x(n

)

FIGURE(2)

0 5 10 15 20 25 30 350

1

2

3

frequency(Hz)

Am

plitu

de

i/p signal in frequency domain

0 5 10 15 20 25 30 350.5

1

1.5

2

2.5

frequency(Hz)

Am

plitu

de

sampled signal in frequency domain

Page 27: Dc Assignment

MESSAGE BOX

FIGURE (3) (SAMPLE BY SAMPLE RECONSTRUCTION)

>AT FIRST SAMPLE

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5

t

x(t)

Original signal

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5Sample by sample reconstruction

Page 28: Dc Assignment

>AT FIFTH SAMPLE

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5

t

x(t

)

Original signal

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5Sample by sample reconstruction

>AT EIGHTH SAMPLE

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5

t

x(t

)

Original signal

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5Sample by sample reconstruction

Page 29: Dc Assignment

>AT ELEVENTH SAMPLE

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5

t

x(t

)

Original signal

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5Sample by sample reconstruction

>AT FIFTEENTH SAMPLE

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5

t

x(t

)

Original signal

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5Sample by sample reconstruction

>AT EIGHTEENTH SAMPLE

Page 30: Dc Assignment

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5

t

x(t

)

Original signal

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5Sample by sample reconstruction

>AT 20th SAMPLE

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5

t

x(t)

Original signal

0 2 4 6 8 10 12 14 16 18 20-0.5

0

0.5Reconstruction finished

FIGURE (4)

Page 31: Dc Assignment

0 5 10 15 20 25 30 352

4

6

8

10

frequency(Hz)

Am

plitu

de

reconstructed signal in frequency domain

0 5 10 15 20 25 30 350

1

2

3

frequency(Hz)

Am

plitu

de

i/p signal in frequency domain

*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

Page 32: Dc Assignment