100% found this document useful (2 votes)
333 views

Matlab Code

The document presents MATLAB code to sample an intelligence signal of 5000 Hz frequency at different sampling clock frequencies (16000 Hz, 9600 Hz, 6000 Hz, 4000 Hz), reconstruct the sampled signal using single and double filters, and compare the reconstructed signals. Figures are generated to plot the original signal, clock signals, sampled signals, and reconstructed signals for each sampling frequency.

Uploaded by

Rakib Hyder
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
333 views

Matlab Code

The document presents MATLAB code to sample an intelligence signal of 5000 Hz frequency at different sampling clock frequencies (16000 Hz, 9600 Hz, 6000 Hz, 4000 Hz), reconstruct the sampled signal using single and double filters, and compare the reconstructed signals. Figures are generated to plot the original signal, clock signals, sampled signals, and reconstructed signals for each sampling frequency.

Uploaded by

Rakib Hyder
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

MATLAB Code for Steps 1&2 :

clear all;close all;clc;clf;


f=1000; %%%% Frequency of Intelligence Signal
fs=4000; %%%% Sampling Clock Frequency
step=1/(10000*f);
stepsh=1/(10000*fs);
t=0:step:3/f;
tsh=0:stepsh:3/f;
x=1.5*sin(2*pi*f*t);
clk=([square(2*pi*fs*t,50)]==1); %% Sampling Clock Signal
samp_hold=zeros(1,length(t));
samp=x.*clk; %%% Sampled Signal
set=0; %%% Memory Set
for i=1:length(t) %%% Block for Sample and Hold
if set==0 && clk(i)==1
set=1;
tmp=x(i);
elseif set==1 && clk(i)==0
set=0;
tmp=0;
end
samp_hold(i)=tmp;
end
[n1,d1]=butter(1,4*f*step,'low'); %Filters
recon_samp1=filter(n1,d1,samp);
recon_samp2=filter(n1,d1,recon_samp1);
recon_samp1=max(x)/max(recon_samp1)*recon_samp1; %%% Amplifiers
recon_samp2=max(x)/max(recon_samp2)*recon_samp2;
[n2,d2]=butter(1,4*f*step,'low');
recon_samp_hold1=filter(n2,d2,samp_hold);
recon_samp_hold2=filter(n2,d2,recon_samp_hold1);
recon_samp_hold1=max(x)/max(recon_samp_hold1)*recon_samp_hold1;
recon_samp_hold2=max(x)/max(recon_samp_hold2)*recon_samp_hold2;
figure(1);subplot(2,1,1)
plot(t,x)
xlabel('Time(in seconds)','color','b');
ylabel('x(t)','color','b');
title('\bfIntelligence Signal','fontsize',12);
subplot(2,1,2)
plot(t,clk)
ylim([-0.5 1.5])
xlabel('Time(in seconds)','color','b');
ylabel('clk(t)','color','b');
title('\bfClock Signal','fontsize',12);
figure(2);subplot(3,1,1)
plot(t,samp)
xlabel('Time(in seconds)','color','b');
ylabel('samp(t)','color','b');
title(['\bfNatural Sampling at ',num2str(fs),'Hz'],'fontsize',12);
subplot(3,1,2)
plot(t,recon_samp1)
xlabel('Time(in seconds)','color','b');
ylabel('recon-samp1(t)','color','b');
title('\bfReconstructed Signal(Single filter)','fontsize',12);
subplot(3,1,3)
plot(t,recon_samp2)
xlabel('Time(in seconds)','color','b');
ylabel('recon-samp2(t)','color','b');
title('\bfReconstructed Signal(Double filter)','fontsize',12);
figure(3);subplot(3,1,1)
plot(t,samp_hold)
xlabel('Time(in seconds)','color','b');
ylabel('samp-hold(t)','color','b');
title(['\bfFlat-top Sampling at ',num2str(fs),'Hz'],'fontsize',12);
subplot(3,1,2)
plot(t,recon_samp_hold1)
xlabel('Time(in seconds)','color','b');
ylabel('recon-samp-hold1(t)','color','b');
title('\bfReconstructed Signal(Single filter)','fontsize',12);
subplot(3,1,3)
plot(t,recon_samp_hold2)
xlabel('Time(in seconds)','color','b');
ylabel('recon-samp-hold2(t)','color','b');
title('\bfReconstructed Signal(Double filter)','fontsize',12);

Output:
Intelligence Signal of 1.5V (p-p), 1 kHz and clock signal with 4kHz frequency.
Figure 1

0 0.5 1 1.5 2 2.5 3
x 10
-3
-2
-1
0
1
2
Time(in seconds)
x
(
t
)
Intelligence Signal
0 0.5 1 1.5 2 2.5 3
x 10
-3
-0.5
0
0.5
1
1.5
Time(in seconds)
c
l
k
(
t
)
Clock Signal
Figure 2

Figure 3

0 0.5 1 1.5 2 2.5 3
x 10
-3
-2
0
2
Time(in seconds)
s
a
m
p
(
t
)
Natural Sampling at 4000Hz
0 0.5 1 1.5 2 2.5 3
x 10
-3
-2
0
2
Time(in seconds)
r
e
c
o
n
-
s
a
m
p
1
(
t
)
Reconstructed Signal(Single filter)
0 0.5 1 1.5 2 2.5 3
x 10
-3
-2
0
2
Time(in seconds)
r
e
c
o
n
-
s
a
m
p
2
(
t
)
Reconstructed Signal(Double filter)
0 0.5 1 1.5 2 2.5 3
x 10
-3
-2
0
2
Time(in seconds)
s
a
m
p
-
h
o
l
d
(
t
)
Flat-top Sampling at 4000Hz
0 0.5 1 1.5 2 2.5 3
x 10
-3
-2
0
2
Time(in seconds)
r
e
c
o
n
-
s
a
m
p
-
h
o
l
d
1
(
t
)
Reconstructed Signal(Single filter)
0 0.5 1 1.5 2 2.5 3
x 10
-3
-2
0
2
Time(in seconds)
r
e
c
o
n
-
s
a
m
p
-
h
o
l
d
2
(
t
)
Reconstructed Signal(Double filter)
MATLAB Code for Step 3:
clear all;close all;clc;clf;
f=5000; %%%% Frequency of Intelligence Signal
step=1/(10000*f);
t=0:step:10/f;
x=1.5*sin(2*pi*f*t);
figure(1)
plot(t,x)
xlabel('Time(in seconds)','color','b');
ylabel('x(t)','color','b');
title(['\bfIntelligence Signal of ',num2str(f),' Hz'],'fontsize',12);

%%% 16000Hz
fs=16000; %%%% Sampling Clock Frequency
stepsh=1/(10000*fs);
tsh=0:stepsh:3/f;
clk=([square(2*pi*fs*t,50)]==1); %% Sampling Clock Signal
samp_hold=zeros(1,length(t));
samp=x.*clk; %%% Sampled Signal
set=0; %%% Memory Set
for i=1:length(t) %%% Block for Sample and Hold
if set==0 && clk(i)==1
set=1;
tmp=x(i);
elseif set==1 && clk(i)==0
set=0;
tmp=0;
end
samp_hold(i)=tmp;
end
[n1,d1]=butter(1,4*f*step,'low'); %Filters
recon_samp1=filter(n1,d1,samp);
recon_samp2=filter(n1,d1,recon_samp1);
recon_samp1=max(x)/max(recon_samp1)*recon_samp1; %%% Amplifiers
recon_samp2=max(x)/max(recon_samp2)*recon_samp2;
[n2,d2]=butter(1,4*f*step,'low');
recon_samp_hold1=filter(n2,d2,samp_hold);
recon_samp_hold2=filter(n2,d2,recon_samp_hold1);
recon_samp_hold1=max(x)/max(recon_samp_hold1)*recon_samp_hold1;
recon_samp_hold2=max(x)/max(recon_samp_hold2)*recon_samp_hold2;
figure(2),
subplot(3,1,1)
plot(t,clk)
ylim([-0.5 1.5])
xlabel('Time(in seconds)','color','b');
ylabel('clk(t)','color','b');
title(['\bfClock Signal of ',num2str(fs), 'Hz'],'fontsize',12);
subplot(3,1,2)
plot(t,recon_samp_hold1)
xlabel('Time(in seconds)','color','b');
ylabel('recon-samp-hold1(t)','color','b');
title('\bfReconstructed Signal(Single filter)','fontsize',12);
subplot(3,1,3)
plot(t,recon_samp_hold2)
xlabel('Time(in seconds)','color','b');
ylabel('recon-samp-hold2(t)','color','b');
title('\bfReconstructed Signal(Double filter)','fontsize',12);

%%% 9600Hz
fs=9600; %%%% Sampling Clock Frequency
step=1/(10000*f);
stepsh=1/(10000*fs);
tsh=0:stepsh:3/f;
clk=([square(2*pi*fs*t,50)]==1); %% Sampling Clock Signal
samp_hold=zeros(1,length(t));
samp=x.*clk; %%% Sampled Signal
set=0; %%% Memory Set
for i=1:length(t) %%% Block for Sample and Hold
if set==0 && clk(i)==1
set=1;
tmp=x(i);
elseif set==1 && clk(i)==0
set=0;
tmp=0;
end
samp_hold(i)=tmp;
end
[n1,d1]=butter(1,4*f*step,'low'); %Filters
recon_samp1=filter(n1,d1,samp);
recon_samp2=filter(n1,d1,recon_samp1);
recon_samp1=max(x)/max(recon_samp1)*recon_samp1; %%% Amplifiers
recon_samp2=max(x)/max(recon_samp2)*recon_samp2;
[n2,d2]=butter(1,4*f*step,'low');
recon_samp_hold1=filter(n2,d2,samp_hold);
recon_samp_hold2=filter(n2,d2,recon_samp_hold1);
recon_samp_hold1=max(x)/max(recon_samp_hold1)*recon_samp_hold1;
recon_samp_hold2=max(x)/max(recon_samp_hold2)*recon_samp_hold2;
figure(3),
subplot(3,1,1)
plot(t,clk)
ylim([-0.5 1.5])
xlabel('Time(in seconds)','color','b');
ylabel('clk(t)','color','b');
title(['\bfClock Signal of ',num2str(fs), 'Hz'],'fontsize',12);
subplot(3,1,2)
plot(t,recon_samp_hold1)
xlabel('Time(in seconds)','color','b');
ylabel('recon-samp-hold1(t)','color','b');
title('\bfReconstructed Signal(Single filter)','fontsize',12);
subplot(3,1,3)
plot(t,recon_samp_hold2)
xlabel('Time(in seconds)','color','b');
ylabel('recon-samp-hold2(t)','color','b');
title('\bfReconstructed Signal(Double filter)','fontsize',12);

%%% 6000Hz
fs=6000; %%%% Sampling Clock Frequency
step=1/(10000*f);
stepsh=1/(10000*fs);
tsh=0:stepsh:3/f;
clk=([square(2*pi*fs*t,50)]==1); %% Sampling Clock Signal
samp_hold=zeros(1,length(t));
samp=x.*clk; %%% Sampled Signal
set=0; %%% Memory Set
for i=1:length(t) %%% Block for Sample and Hold
if set==0 && clk(i)==1
set=1;
tmp=x(i);
elseif set==1 && clk(i)==0
set=0;
tmp=0;
end
samp_hold(i)=tmp;
end
[n1,d1]=butter(1,4*f*step,'low'); %Filters
recon_samp1=filter(n1,d1,samp);
recon_samp2=filter(n1,d1,recon_samp1);
recon_samp1=max(x)/max(recon_samp1)*recon_samp1; %%% Amplifiers
recon_samp2=max(x)/max(recon_samp2)*recon_samp2;
[n2,d2]=butter(1,4*f*step,'low');
recon_samp_hold1=filter(n2,d2,samp_hold);
recon_samp_hold2=filter(n2,d2,recon_samp_hold1);
recon_samp_hold1=max(x)/max(recon_samp_hold1)*recon_samp_hold1;
recon_samp_hold2=max(x)/max(recon_samp_hold2)*recon_samp_hold2;
figure(4),
subplot(3,1,1)
plot(t,clk)
ylim([-0.5 1.5])
xlabel('Time(in seconds)','color','b');
ylabel('clk(t)','color','b');
title(['\bfClock Signal of ',num2str(fs), 'Hz'],'fontsize',12);
subplot(3,1,2)
plot(t,recon_samp_hold1)
xlabel('Time(in seconds)','color','b');
ylabel('recon-samp-hold1(t)','color','b');
title('\bfReconstructed Signal(Single filter)','fontsize',12);
subplot(3,1,3)
plot(t,recon_samp_hold2)
xlabel('Time(in seconds)','color','b');
ylabel('recon-samp-hold2(t)','color','b');
title('\bfReconstructed Signal(Double filter)','fontsize',12);

%%% 4000Hz
fs=4000; %%%% Sampling Clock Frequency
step=1/(10000*f);
stepsh=1/(10000*fs);
tsh=0:stepsh:3/f;
clk=([square(2*pi*fs*t,50)]==1); %% Sampling Clock Signal
samp_hold=zeros(1,length(t));
samp=x.*clk; %%% Sampled Signal
set=0; %%% Memory Set
for i=1:length(t) %%% Block for Sample and Hold
if set==0 && clk(i)==1
set=1;
tmp=x(i);
elseif set==1 && clk(i)==0
set=0;
tmp=0;
end
samp_hold(i)=tmp;
end
[n1,d1]=butter(1,4*f*step,'low'); %Filters
recon_samp1=filter(n1,d1,samp);
recon_samp2=filter(n1,d1,recon_samp1);
recon_samp1=max(x)/max(recon_samp1)*recon_samp1; %%% Amplifiers
recon_samp2=max(x)/max(recon_samp2)*recon_samp2;
[n2,d2]=butter(1,4*f*step,'low');
recon_samp_hold1=filter(n2,d2,samp_hold);
recon_samp_hold2=filter(n2,d2,recon_samp_hold1);
recon_samp_hold1=max(x)/max(recon_samp_hold1)*recon_samp_hold1;
recon_samp_hold2=max(x)/max(recon_samp_hold2)*recon_samp_hold2;
figure(5)
subplot(3,1,1)
plot(t,clk)
ylim([-0.5 1.5])
xlabel('Time(in seconds)','color','b');
ylabel('clk(t)','color','b');
title(['\bfClock Signal of ',num2str(fs), 'Hz'],'fontsize',12);
subplot(3,1,2)
plot(t,recon_samp_hold1)
xlabel('Time(in seconds)','color','b');
ylabel('recon-samp-hold1(t)','color','b');
title('\bfReconstructed Signal(Single filter)','fontsize',12);
subplot(3,1,3)
plot(t,recon_samp_hold2)
xlabel('Time(in seconds)','color','b');
ylabel('recon-samp-hold2(t)','color','b');
title('\bfReconstructed Signal(Double filter)','fontsize',12);

Output:
Figure 1

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
x 10
-3
-1.5
-1
-0.5
0
0.5
1
1.5
Time(in seconds)
x
(
t
)
Intelligence Signal of 5000 Hz
Figure 2

Figure 3


0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
x 10
-3
-0.5
0
0.5
1
1.5
Time(in seconds)
c
l
k
(
t
)
Clock Signal of 16000Hz
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
x 10
-3
-2
0
2
Time(in seconds)
r
e
c
o
n
-
s
a
m
p
-
h
o
l
d
1
(
t
)
Reconstructed Signal(Single filter)
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
x 10
-3
-2
0
2
Time(in seconds)
r
e
c
o
n
-
s
a
m
p
-
h
o
l
d
2
(
t
)
Reconstructed Signal(Double filter)
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
x 10
-3
-0.5
0
0.5
1
1.5
Time(in seconds)
c
l
k
(
t
)
Clock Signal of 9600Hz
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
x 10
-3
-2
0
2
Time(in seconds)
r
e
c
o
n
-
s
a
m
p
-
h
o
l
d
1
(
t
)
Reconstructed Signal(Single filter)
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
x 10
-3
-2
0
2
Time(in seconds)
r
e
c
o
n
-
s
a
m
p
-
h
o
l
d
2
(
t
)
Reconstructed Signal(Double filter)
Figure 4

Figure 5

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
x 10
-3
-0.5
0
0.5
1
1.5
Time(in seconds)
c
l
k
(
t
)
Clock Signal of 6000Hz
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
x 10
-3
-2
0
2
Time(in seconds)
r
e
c
o
n
-
s
a
m
p
-
h
o
l
d
1
(
t
)
Reconstructed Signal(Single filter)
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
x 10
-3
-2
0
2
Time(in seconds)
r
e
c
o
n
-
s
a
m
p
-
h
o
l
d
2
(
t
)
Reconstructed Signal(Double filter)
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
x 10
-3
-0.5
0
0.5
1
1.5
Time(in seconds)
c
l
k
(
t
)
Clock Signal of 4000Hz
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
x 10
-3
-2
0
2
Time(in seconds)
r
e
c
o
n
-
s
a
m
p
-
h
o
l
d
1
(
t
)
Reconstructed Signal(Single filter)
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
x 10
-3
-2
0
2
Time(in seconds)
r
e
c
o
n
-
s
a
m
p
-
h
o
l
d
2
(
t
)
Reconstructed Signal(Double filter)

You might also like