You are on page 1of 16

Matlab code for unit impulse signal generation:

clc;
clear all;
close all;
disp('Unit Impulse Signal Generation');
N=input('Enter no of samples: ');
n=-N:1:N;
x=[zeros(1,N),1,zeros(1,N)];
stem(n,x);
xlabel('Sample');
ylabel('Amplitude');
title('Unit Impulse Signal');

In this, the impulse is generated by using ZEROS(x,y) function, which
produces an array of size X,Y with all elements as ZERO.


Matlab code for unit ramp signal generation:

clc;

clear all;
close all;
disp('Unit Ramp Signal Generation');
N=input('Enter no of samples: ');
a=input(' Max Amplitude: ');
n=-N:1:N;
x=a*n/N;
stem(n,x);
xlabel('Sample');
ylabel('Amplitude');
title('Unit Ramp Signal');


Matlab code for unit step (delayed step) signal generation:

clc;

clear all;

close all;
disp('Delayed Unit Step Signal Generation');
N=input('Enter no of samples: ');
d=input('Enter delay value: ');
n=-N:1:N;
x=[zeros(1,N+d),ones(1,N-d+1)];
stem(n,x);
xlabel('Sample');
ylabel('Amplitude');
title('Delayed Unit Step Signal');


Matlab code for discrete sinusoidal signal generation:
clc;
clear all;
close all;
disp('Sinusoidal Signal generation');
N=input('Enter no of samples: ');
n=0:0.1:N;
x=sin(n);
figure, stem(n,x);
xlabel('Samples');
ylabel('Amplitude');
title('Sinusoidal Signal');

The SIN(n) function returns an array which corresponds to sine value of the
array n


Matlab code for discrete cosine signal generation:

clc;
clear all;
close all;
disp('Cosine wave generation');
N=input('Enter no of samples');
n=0:0.1:N;
x=cos(n);
figure, stem(n,x);
xlabel('Samples');
ylabel('Amplitude');
title('Cosine');


The COS(n) function returns an array which corresponds to cosine value of
the array n


clc;clear all;
n=input('Enter the no samples: ');
x=0:0.1/n:20;

s=sawtooth(x);
t=sawtooth(x,0.5); % width=0.5 for Triangular signal

subplot(2,1,1),
plot(x,s),
xlabel('Time'),
ylabel('Amplitude'),
title('Sawtooth signal');

subplot(2,1,2),
plot(x,t),title('Triangular signal'),
xlabel('Time'),
ylabel('Amplitude');


Matlab code for exponentially decaying signal generation:


clc;
clear all;
close all;
disp('Exponential decaying signal');
N=input('Enter no of samples: ');

a=1;
t=0:0.1:N;
x=a*exp(-t);
figure,plot(t,x);
xlabel('Time');
ylabel('Amplitude');
title('Exponentially Decaying Signal');


Matlab code for exponentially growing signal generation:
clc;
clear all;
close all;
disp('Exponential growing signal');
N=input('Enter no of samples: ');
a=1;
t=0:0.1:N;
x=a*exp(t);
figure,stem(t,x);
xlabel('Time');
ylabel('Amplitude');
title('Exponentially Decaying Signal');


http://www.elecdude.com/2013/01/matlab-code-for-circular-convolution.html


MATLAB PROGRAM TO DISPLAY THE PROPERTIES OF DISCRETE
FOURIER TRANSFORM (DFT) - LINEARITY PROPERTY


%DFT linearity property
close all;
clear all;
N=input('Howmany point DFT do you want?');
x1=input('Enter the first sequence=');
x2=input('Enter the second sequence=');
a=input('Enter the first constant a=');
b=input('Enter the second constant b=');
n1=length(x1);
n2=length(x2);
c=zeros(N);
x1=[x1 zeros(1,N-n1)];%make both x1 and x2
x2=[x2 zeros(1,N-n2)];%of same dimension
x3=a*x1
x4=b*x2
x5=x3+x4 %a*x1+b*x2
for k=1:N
for n=1:N
w=exp((-2*pi*i*(k-1)*(n-1))/N);
%prev.step=>evaluating w-matrix
x(n)=w;
end
c(k,:)=x; %Every row of w-matrix is inserted &
end %finally c becomes the w matrix
%for n-point DFT

r1=c*x1'; %x1(k)
r2=c*x2'; %x2(k)
R3=a*r1+b*r2 %a*x1(k)+b*x2(k)
R4=c*x5' %DFT of a*x1(n)+b*x2(n)
%plotting magnitude and angle
subplot(211)
stem(abs(R4));
title('DFT of {a*x1(n)+b*x2(n)}');
subplot(212)
stem(angle(R3));
title('DFT of {a*x1(k)+b*x2(k)}');


MATLAB PROGRAM TO DISPLAY THE PROPERTIES OF DISCRETE
FOURIER TRANSFORM (DFT) - MULTIPLICATION PROPERTY
%multiplication property x1(n)*x2(n)=(1/N)*circonv(X1(k)*X2(k))
clear all;
x1=input('enter the sequence=');
x2=input('enter the second seq of same length=');
N9=input ('enter the number of samples=');
x3=(x1).*(x2); %multiplication of 2 signals
x4=fft(x1,N9);%dft of first seq
N1=length(x4);%length of sequence
x5=fft(x2,N9);%dft of secnd sequence
N2=length(x5);%length of second sequence
%finding circonvolution of 2 signals
x=x4;
h=x5;
N1=length(x);
N2=length(h);
N=max(N1,N2);
x=[x zeros(1,N-N1)];
h=[h zeros(1,N-N2)];
for n=0:N-1
y(n+1)=0;
for i=0:N-1
j=mod(n-i,N);
y(n+1)=y(n+1)+x(i+1)*h(j+1);
end
end
y=y/N; %rhs
x6=fft(x3,N); %lhs
n=0:1:N-1;
subplot(121);
stem(n,x6);
title('dft of 2 multiplied signals');
grid on;
subplot(122);
stem(n,y);
title('Nth part of circular convolution of 2 dfts');
grid on;


MATLAB PROGRAM TO IMPLEMENT THE PROPERTIES OF DISCRETE
FOURIER TRANSFORM (DFT) - FREQUENCY SHIFT PROPERTY
%dft frequecy shift property
close all;
clear all;
N=input('how many point dft do you want?');
x1=input('enter the seq');
n2=length(x1);
c=zeros(N);
x1=[x1 zeros(1,N-n2)];
for k=1:N
for n=1:N
w=exp((-2*pi*i*(k-1)*(n-1))/N);
x(n)=w;
end
c(k,:)=x;
end
disp('dft is ');
r=c*x1';
a1=input('enter the amount of shift in frequency domain');
for n=1:N
w=exp((2*pi*i*(n-1)*(a1))/N);
x2(n)=w;
end
r1=x2.*x1;
subplot(221);
stem(abs(r));
grid on;
title('orginal dft magnitude plot');
subplot(222);
stem(angle(r));
grid on;
title('orginal dft angle');
for k=1:N
for n=1:N
w=exp((-2*pi*i*(k-1)*(n-1))/N);
x(n)=w;
end
c(k,:)=x;
end
disp('dft is');
r2=c*r1';
subplot(223);
stem(abs(r2));
grid on;
title('shifted dft magnitude');
subplot(224);
stem(angle(r2));
grid on;
title('shifed dft angle');


MATLAB PROGRAM TO IMPLEMENT THE PROPERTIES OF DISCRETE
FOURIER TRANSFORM (DFT) - CONVOLUTION PROPERTY

clear all;
x= input ('enter the first sequence');
h= input ('enter the second sequence');
No=input('number of samples?');
N1= length(x);
N2= length(h);
N=max(N1,N2);%length of sequence
x=[x zeros(1,N-N1)]; %modified first sequence
h=[h zeros(1,N-N2)]; %modified second sequence

for n=0:N-1;
y(n+1)=0;
for i=0:N-1
j=mod(n-i,N);
y(n+1)=y(n+1)+x(i+1)*h(j+1); %shifting and adding
end
end
n=0:1:No-1;
x4=fft(x);
x5=fft(h);
x6=fft(y);
%seq 2
subplot(121)
stem(n,x6);
title('dft of two convoluted signals');
x7=(x4).*(x5);%product of two dfts
subplot(122);
stem(n,x7);
title('product of two dfts');
grid on;


EFFECT OF RECTANGULAR | HAMMING | HANNING | BLACK MAN
WINDOWS ON FIR lowpass filter
clear all;
close all;
f=input('enter the freq');
N=input('enter the number of samples');
w=2*pi*f;
alpha=(N-1)/2;
h=zeros(1,N);
for n=0:1:N-1
if n~=(N-1)/2
h(n+1)=sin(w*(n-alpha))/((n-alpha)*pi);
h(n+1)=1-h(n+1);
end
end
h(((N-1)/2)+1)=w/pi;
rectangular_window=boxcar(N);
ham=hamming(N);
han=hanning(N);
black=blackman(N);
h1=h.*rectangular_window';
h2=h.*ham';
h3=h.*han';
h4=h.*black';
w=0:.01:pi;
H1=freqz(h1,1,w);
H2=freqz(h2,1,w);
H3=freqz(h3,1,w);
H4=freqz(h4,1,w);
plot(w/pi,abs(H1),'r',w/pi,abs(H2),'g',w/pi,abs(H3),'y',w/pi,abs(H4));

OUTPUT
enter the freq 0.3*pi
enter the samples 33

You might also like