Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

MATLAB

MATrix LABoratory
Sujith R
Asst. Professor
ECE Dept, KRGCE

Sample Programs
Generation of signals

%sine & cosine wave generation


clc
clear all
close all
t=-pi:.05:pi;
y=sin(2*pi*t);
subplot(2,1,1);
plot(t,y);
xlabel('time')
ylabel('amplitude')
title('sine wave');
y=cos( 2*pi*t);
subplot(2,1,2);
plot(t,y);
xlabel('time')
ylabel('amplitude')
title(' cosine wave');
%unit impulse signal
clc
clear all
close all
n=input('enter the value of n:');
t=-n:n;
y=[zeros(1,n),1,zeros(1,n)];
stem(t,y);
xlabel('time');
ylabel('amplitude');
title('unit impulse signal');

output:
enter the value of n:2
%unit step signal
clc
clear all
close all
n=input('Enter the value of n: ');
t=-2:n-1;
y=[zeros(1,2),ones(1,n)];
stem(t,y);
xlabel('time');
ylabel('amplitude');
title('unit step signal');

output:
'Enter the value of n:8

%ramp signal
clc
clear all
close all
n=input('enter the value of n:');
t=0:n-1;
y=t;
stem(t,y);
xlabel('time');
ylabel('amplitude');
title('ramp signal');

output:
enter the value of n:5

%exponential signal
clc
clear all
close all
n=input('enter the value of n:');
t=0:n-1;
y=exp (t);
stem(t,y);
xlabel('time');
ylabel('amplitude');
title('exponential signal');

output:
enter the value of n:10
%linear convolution
clc
clear all
close all
x=input('enter the first sequence');
h=input('enter the second sequence');
y=conv(x,h);
subplot(3,1,1);
stem(x);
xlabel('n');
ylabel('x(n)')
title('first sequence');
subplot(3,1,2);
stem(h);
xlabel('n');
ylabel('h(n)');
title('second sequence');

subplot(3,1,3);
stem(y);
xlabel('n');
ylabel('y(n)');
title('linear convolution');
Output:

enter the first sequence[1,2,3]


enter the second sequence[2,4,6]
%circular convolution using zero padding method
clc
clear all
close all
g=input('enter the first sequence');
h=input('enter the second sequence');
N=input('enter the number of points in the output sequence');
N1=length(g);
N2=length(h);
g=[g zeros(1,N-N1)];
h=[h zeros(1,N-N2)];
for n=1:N;
y(n)=0;
for i=1:N;
j=n-i+1
if(j<=0)
j=N+j;
end
y(n)=y(n)+g(i)*h(j)
end
end
subplot(3,1,1);
stem(g);
xlabel('n');
ylabel('g(n)');
title('first sequence');
subplot(3,1,2);
stem(h);
xlabel('n');
ylabel('h(n)');
title('second sequence');
subplot(3,1,3)
stem(y);
xlabel('n');
ylabel('y(n)');
title('circular convolution with zero padding method');
Output:
enter the first sequence[1,2,3]
enter the second sequence[1,2]
enter the number of points in the output sequence4

%circular convolution
clc
clear all
close all
g=input('enter the first sequence');
h=input('enter the second sequence');
N=input('enter the number of points in the output sequence');
N1=length(g);
N2=length(h);
for n=1:N
y(n)=0;
for i=1:N;
j=n-i+1;
if(j<=0)
j=N+j
end
y(n)=y(n)+g(i)*h(j)
end
end
subplot(3,1,1);
stem(g);
xlabel('n');
ylabel('g(n)');
title('first sequence');
subplot(3,1,2);
stem(h);
xlabel('n');
ylabel('h(n)');
title('second sequence');
subplot(3,1,3);
stem(y);
xlabel('n');
ylabel('y(n)');
title('circular convolution');
Output:
enter the first sequence[3,4,2,1]
enter the second sequence[1,2,3,0]
enter the number of points in the output sequence4
%Generation of DFT Sequence
clear all;
close all;
x=input('enter the input sequence');
n=input('enter the length of FFT');
X=fft(x,n);
subplot(3,1,1);
stem(x);
xlabel('n');
ylabel('x(n)');
title('Input Sequence');
subplot(3,1,2);
stem(X);

xlabel('k');
ylabel('imaginary axis');
display('the resultant signal is');X
y=real(X);
z=imag(X);
subplot(3,1,3);
stem(y,z);
xlabel('real axis');
ylabel('imaginary axis');
title('DFT Sequence');
Output:
enter the first sequence[1,2,3,4,4,3,2,1]
enter the length of FFT8
the resultant signal is
X =
Columns 1 through 5
20.0000
0.4142i

-5.8284 - 2.4142i

Columns 6 through 8
-0.1716 + 0.4142i

-5.8284 + 2.4142i

% IIR lowpass Butterworth filter


clear all;
close all
rp=input('Enter the passband ripple in db');
rs=input('Enter the stopband ripple in db');
fp=input('Enter the passband frequency in Hz');
fs=input('Enter the stopband frequency in Hz');
F=input('Enter the sampling frequency in Hz');
omp=2*fp/F;
oms=2*fs/F;
[n,wn]=buttord(omp,oms,rp,rs);
[b,a]=butter(n,wn);
w=0:.01:pi;
[h,om]=freqz(b,a,w,'whole');
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('gain db');
xlabel('normalised frequency');
subplot(2,1,2);
plot(om/pi,an);
xlabel('normalised frequency');
ylabel('phase in radians');

-0.1716 -

Output:
Enter the
Enter the
Enter the
Enter the
Enter the

passband
stopband
passband
stopband
sampling

ripple in
ripple in
frequency
frequency
frequency

db
db
in
in
in

.4
30
Hz 400
Hz 800
Hz 2000

% IIR highpass Butterworth filter


clear all;
close all;
rp=input('Enter the passband ripple in dB');
rs=input('Enter the stopband ripple in dB');
fp=input('Enter the passband frequency in Hz');
fs=input('Enter the stopband frequency in Hz');
F=input('Enter the sampling frequency in Hz');
omp=2*fp/F;
oms=2*fs/F;
[n,wn]=buttord(omp,oms,rp,rs);
[b,a]=butter(n,wn,'high');
w=0:.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('Gain in dB');
xlabel('Normalised frequency');
subplot(2,1,2);
plot(om/pi,an);
xlabel('Normalised frequency');
ylabel('Phase in radians');
Output:
Enter
Enter
Enter
Enter
Enter

the
the
the
the
the

passband
stopband
passband
stopband
sampling

ripple in
ripple in
frequency
frequency
frequency

dB
dB
in
in
in

.4
30
Hz 400
Hz 800
Hz 2000

%FIR Lowpass Filter using window method


clear all;
close all;
rp= input('Enter passband ripple in dB');
rs=input('Enter stopband ripple in dB');
fp=input('Enter passband frequency in Hz');
fs=input('Enter stopband frequency in Hz');
f=input('Enter sampling frequency in Hz');
wp=2*fp/f;
ws=2*fs/f;

num=-20*log10(sqrt(rp*rs))-13;
den=14.6*(fs-fp)/f;
n=ceil(num/den);
n1=n+1;
if (rem(n,2)~=0);
n1=n;
n=n-1;
end
y =boxcar(n1);
b=fir1(n,wp,y);
[h,om]=freqz(b,1,256);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('Gain in dB');
xlabel('Normalised frequency');
subplot(2,1,2);
plot(om/pi,an);
ylabel('Phase in radians');
xlabel('Normalised frequency');

Output:
Enter passband
Enter stopband
Enter passband
Enter stopband
Enter sampling

ripple in
ripple in
frequency
frequency
frequency

dB .05
dB.04
in Hz 1500
in Hz 2000
in Hz 9000

%FIR Highpass Filter Using Window method


clear all
close all
rp= input('Enter passband ripple in dB');
rs=input('Enter stopband ripple in dB');
fp=input('Enter passband frequency in Hz');
fs=input('Enter stopband frequency in Hz');
f=input('enter sampling frequency in Hz');
wp=2*fp/f;
ws=2*fs/f;
num=-20*log10(sqrt(rp*rs))-13;
den=14.6*(fs-fp)/f;
n=ceil(num/den);
n1=n+1;
if (rem(n,2)~=0);
n1=n;
n=n-1;
end
y =boxcar(n1);
b=fir1(n,wp,'high',y);
[h,om]=freqz(b,1,256);

m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('Gain in dB');
xlabel('Normalised frequency');
subplot(2,1,2);
plot(om/pi,an);
ylabel('Phase in radians');
xlabel('Normalised frequency');
Output:
Enter passband
Enter stopband
Enter passband
Enter stopband
Enter sampling

ripple in
ripple in
frequency
frequency
frequency

dB .05
dB.04
in Hz 1500
in Hz 2000
in Hz 9000

%generation of am signal
close all
clear all
fm=20;
fc=500;
vm=1;
vc=1;
interval=0.001;
t=0:0.00001:0.09999;
f=0:1:9999;
wc=2*pi*fc;
wm=2*pi*fm;
V1=vc+vm*sin(wm*t);
V2=-(vc+vm*sin(wm*t));
Vm=vm*sin(wm*t);
Vc=vc*sin(wc*t);
Vam=(1+sin(wm*t)).*(sin(wc*t));
Vf=abs(fft(Vam,10000))/10000;
subplot(3,1,3);
plot(t,Vam);
hold on;
plot(t,V1,'r'),plot(t,V2,'r');
title('AM waveform time-domain');
xlabel('time'), ylabel('amplitude');
grid on;
subplot(3,1,1);
plot(t,Vm);
title('AM modulating signal');
xlabel('time'), ylabel('amplitude');
grid on;
subplot(3,1,2);
plot(t, Vc);
title('AM carrier signal');

xlabel('time'), ylabel('amplitude');
grid on;
%generation of fm signal
vc=1;
vm=1;
fm=250;
fc=5000;
m=10;
t=0:0.00001:0.09999;
f=0:10:99990;
wc=2*pi*fc;
wm=2*pi*fm;
sc_t=vc*cos(wc*t);
sm_t=vm*cos(wm*t);
s_fm=vc*cos((wc*t)+10*sin(wm*t));
subplot(2,1,2);
plot(t,s_fm);
hold on;
plot(t,sm_t,'r');
axis([0 0.01 -1.5 1.5]);
xlabel('time(second)'),ylabel('amplitude');
title('FM time-domain');
grid on;
subplot(2,1,1);
plot(t,sm_t);
axis([0 0.1 -1.5 1.5]);
title('FM modulating signal');
%Genration of PWM
clc;
clear all;
close all;
t=0:0.001:1;
s=sawtooth(2*pi*10*t+pi);
m=.75*sin(2*pi*1*t);
n=length(s);
for i=1:n
if(m(i)>=s(i))
pwm(i)=1;
elseif(m(i)<=s(i))
pwm(i)=0;
end
end
plot(t,pwm,'-k',t,m,'--r',t,s,'--b');
title('PWM ')
axis([0 1 -1.5 1.5]);

Sincere Thanks To
Mr. Vijeesh Vijayan, Ms. Deepa T K
ECE Dept. KRGCE, Thuravoor

You might also like