Matlab Programs Re
Matlab Programs Re
Matlab Programs Re
% ALIASING
clc;
clear all;
t=-10:0.01:10;
T=4; fm=1/T;
x=cos(2*pi*fm*t);
xn1=cos(2*pi*n1*fm/fs1);
signal with samp freq <2fm
subplot(2,2,1);
didvides the entire plot into 2 rows and 2 coloumns, and plot the graph in first position
plot(t,x); used for ploting the values of t and X in continious form
stem(n1,xn1,'-r');
used for ploting the values of t and X in discrete form
hold on hold the values as it is
plot(n1,xn1);
xlabel('time in sec');
ylabel('x(t)');
subplot(2,2,3); didvides the entire plot into 2 rows and 2 coloumns, and plot the graph in third position
plot(n2,xn2,'-r'); used for ploting the values of n2 and Xn2 in continious form where 'r' represents red colour
hold on
stem(n2,xn2,'*g'); used for ploting the values of n2 and Xn2 in discrete form where 'g' represents green colour
xlabel('n');
ylabel('x(n)');
n3=-20:1:20;
subplot(2,2,4);
hold on
stem(n3,xn3,'-r');
xlabel('n');
ylabel('x(n)');
%LINEAR CONVOLUTION
clc;
clear all;
stem(l1,a);
xlabel('time');
ylabel('amp');
l2=0:length(x)-1;
subplot(1,3,2);
stem(l2,x);
xlabel('time');
ylabel('amp');
l3=0:length(c)-1;
subplot(1,3,3);
stem(l3,c);
xlabel('time');
ylabel('amp');
AT RUN TIME:
%CIRCULAR CONVOLUTION
clc;
clear all;
close all;
disp(c);
l1=0:length(a)-1;
subplot(1,3,1);
stem(l1,a);
ylabel('amp');
l2=0:length(x)-1;
subplot(1,3,2);
stem(l2,x);
xlabel('time');
ylabel('amp');
l3=0:length(c)-1;
subplot(1,3,3);
st(l3,c);
xlabel('time');
ylabel('amp');
title('circular convolution');
AT RUNTIME
24 22 24 30
% DOWN SAMPLING
clear all;
N=50;
n=0:1:N-1;
x=sin(2*pi*n/20)+sin(2*pi*n/15);
m=2;
x1=x(1:m:N);
n1=1:1:N/m;
subplot(2,1,1);
stem(n,x);
xlabel('n');
ylabel('X');
title('input sequence');
subplot(2,1,2);
stem(n1,x1);
xlabel('n1');
ylabel('x1');
%ILLUSTRATION OF UP SAMPLING
clear all;
N=10;
n=0:1:N-1;
x=sin(2*pi*n/10)+sin(2*pi*n/5);
L=3;
x1=[zeros(1,L*N)]; here L*N = 30; so it returns the value zero from the range 1 to 30
n1=1:1:L*N; j=1:L:L*N;
x1(j)=x;
subplot(2,1,1);
stem(n,x);
xlabel('n');
ylabel('X');
title('input sequence');
subplot(2,1,2);
stem(n1,x1);
xlabel('n1');
ylabel('x1');
%BUTTERWORTH LPF
clc;
clear all;
close all;
format long;
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=buttord(w1,w2,rp,rs); used to find the order of the filter 'n' and cutoff freq wn
used to find the transfer fn
coefficients a and b
[b,a]=butter(n,wn);
wn=(w1*w2);
wn=0:0.01:pi;
[h,o]=freqz(b,a,wn);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(o/pi,m);
xlabel('gain in db');
ylabel('normalised frequency');
subplot(2,1,2);
plot(o/pi,an);
xlabel('normalised freqz');
ylabel('phase in radians');
AT RUNTIME:
% FFT sequence
clc;
clear all;
close all;
for k=1:N
z(k)=0;
for n=1:N
z(k)=z(k)+[a(n)*exp(-j)*2*pi*(k-1)*(n-1)/n];
end;
c=abs(z);
d=angle(z);
l1=0:length(a)-1;
subplot(2,2,1);
stem(l1,a);
xlabel('time');
ylabel('amp');
l2=0:length(z)-1;
subplot(2,2,2);
stem(l2,z);
xlabel('time');
ylabel('amp');
l3=0:length(c)-1;
subplot(2,2,3);
stem(l3,c);
xlabel('time');
ylabel('amp');
l4=0:length(d)-1;
subplot(2,2,4);
stem(l4,d);
xlabel('time');
ylabel('amp');
end;
AT RUNTIME:
clc;
clear all;
wc=.5*pi;
N=25;
alpha=(N-1)/2;
eps=.001;
n=0:1:N-1;
hd=sin(wc*(n-alpha+eps))./(pi*(n-alpha+eps));
hn=hd.*wr';%filter coefficients
w=0:.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'-r');
hold on
w=0:.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'-k');
hold on
wb=blackman(N);
w=0:.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'-g');
xlabel('Normalised frequency');
ylabel('Magnitude');
hold off;
clc;
clear all;
N=25;
alpha=(N-1)/2;
eps=.001;
n=0:1:N-1;
hd=(sin(pi*(n-alpha+eps))-sin(wc*(n-alpha)))./(pi*(n-alpha+eps));
hn=hd.*wr';%filter coefficients
w=0:.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'-r');
hold on
w=0:.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'-k');
hold on
w=0:.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'-g');
xlabel('Normalised frequency');
ylabel('Magnitude');
hold off;
clc;
clear all;
N=25;
alpha=(N-1)/2;
eps=.001;
n=0:1:N-1;
hd=(sin(wc2*(n-alpha+eps))-sin(wc1*(n-alpha)))./(pi*(n-alpha+eps));
w=0:.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'-r');
hold on
w=0:.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'-k');
hold on
w=0:.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'-g');
xlabel('Normalised frequency');
ylabel('Magnitude');
hold off;
clc;
clear all;
N=25;
alpha=(N-1)/2;
eps=.001;
n=0:1:N-1;
hd=(sin(wc1*(n-alpha+eps))-sin(wc2*(n-alpha))+sin(pi*(n-alpha+eps)))./(pi*(n-alpha+eps));
hn=hd.*wr';%filter coefficients
w=0:.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'-r');
hold on
hn=hd.*wh';%filter coefficients
w=0:.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'-k');
hold on
wb=blackman(N);
w=0:.01:pi;
h=freqz(hn,1,w);
plot(w/pi,abs(h),'-g');
hold off;