Sunday, 17 September 2017

MATLAB Program for Exponentially Damped Sinusoidal Wave

%program to generate an exponentially damped sinusoidal wave


close all;
clear all;


A=input('Enter the amplitude of the sinusoidal wave A = ');
f= input('Enter the frequency of the sinusoidal wave F = ');
phi=input('Enter the phase angle of sinusoid phi = ');
a=input('Enter the attenuation factor a = ');


f=f*2*pi;
t=0:.001:1;
y=A*sin(f*t + phi).*exp(-a*t);


plot(t,y);
axis([0 1 -2.2 2.2]);


%output


%Enter the amplitude of the sinusoidal wave A = 2
%Enter the frequency of the sinusoidal wave F = 10
%Enter the phase angle of sinusoid phi = 0
%Enter the attenuation factor a = 6


MATLAB Program for Linear Convolution of Two sequences

 conv() function is used for finding the linear convolution of sequences. The program is shown below.


%Program to find the linear convolution of two sequences

x1=input('Enter the first sequence x1(n) = ');
t1=input('Enter the starting time of first sequence t1 = ');
x2=input('Enter the second sequence x2(n) = ');
t2=input('Enter the starting time of second sequence t2 = ');
l1=length(x1);
l2=length(x2);
ln=l1+l2-1;

yn=conv(x1,x2);

a=t1+l1-1;
t=t1:a;
subplot(311);
stem(t,x1);
grid on;
xlabel('time--->');
ylabel('amplitude--->');
TITLE('First sequence');

a=t2+l2-1;
t=t2:a;
subplot(312);
stem(t,x2);
grid on;
xlabel('time--->');
ylabel('amplitude--->');
TITLE('Second sequence');

tn=t1+t2;
a=tn+ln-1;
t=tn:a;
subplot(313);
stem(t,yn);
grid on;
xlabel('time--->');
ylabel('amplitude--->');
TITLE('Convolved output');

%output

%Enter the first sequence x1(n) = [1 2 6 2 3 1 4]
%Enter the starting time of first sequence t1 = -3
%Enter the second sequence x2(n) = [3 1 4 5 2]
%Enter the starting time of second sequence t2 = -1


MATLAB Program for FIR Bandpass Filter

function fir1() is used for the design of FIR filter.You can refer the MATLAB product help for the syntax of these functions. The program for an FIR bandpass filter is given below.

% Program to design FIR bandpass filter .

close all;
clear all;

fp=input('Enter the start and stop frequency of the pass band');
f=input(' Enter the sampling frequency');
n=input(' Enter the order of the filter');

% Normalizing the frequencies

wp=(2/f).*fp;

%Calculation of filter coefficients

b=fir1(n,wp);

%Plotting the filter response

freqz(b,1,500,f);
TITLE('Magnitude and Phase response');


%output
%Enter the start and stop frequency of the pass band[1000 2000]
%Enter the sampling frequency 5000
%Enter the order of the filter 50

MATLAB Program for IIR Chebyschev Type - II Filter

The function cheb2ord() is used to find the filter order and center frequency and cheby2() is used to find the filter coefficients.A low pass filter is implimented.


% Program to design IIR Chebyschev type - II filter


clear all;
close all;


fp=input('Enter the pass band frequency fp   = ');
fs=input('Enter the stop band frequency fs   = ');
rp=input('Enter the pass band attenuation rp = ');
rs=input('Enter the stop band attenuation rs = ');
f=input ('Enter the sampling frequency f     = ');


wp=2*fp/f;
ws=2*fs/f;


[n,wn]=cheb2ord(wp,ws,rp,rs);


[b,a]=cheby2(n,rs,wn,'low');


freqz(b,a,500,f);
TITLE ('Magnitude and phase respose of the IIR Chebyschev type - II filter');


%output
%Enter the pass band frequency fp   = 1000
%Enter the stop band frequency fs   = 1200
%Enter the pass band attenuation rp = .2
%Enter the stop band attenuation rs = 45
%Enter the sampling frequency f     = 3000

MATLAB Program for IIR Chebyschev Type - I Filter

The function cheb1ord() is used to find the filter order and center frequency and cheby1() is used to find the filter coefficients.A low pass filter is implimented.

% Program to design IIR Chebyschev type - I filter


clear all;
close all;


fp=input('Enter the pass band frequency fp   = ');
fs=input('Enter the stop band frequency fs   = ');
rp=input('Enter the pass band attenuation rp = ');
rs=input('Enter the stop band attenuation rs = ');
f=input ('Enter the sampling frequency f     = ');


wp=2*fp/f;
ws=2*fs/f;


[n,wn]=cheb1ord(wp,ws,rp,rs);


[b,a]=cheby1(n,rp,wn,'low');


freqz(b,a,500,f);
TITLE ('Magnitude and phase respose of the IIR Chebyschev type - I filter');


%output
%Enter the pass band frequency fp   = 1000
%Enter the stop band frequency fs   = 1200
%Enter the pass band attenuation rp = .2
%Enter the stop band attenuation rs = 45
%Enter the sampling frequency f     = 3000


MATLAB Program for IIR Butterworth Filter

The function buttord(), is used to find the filter order and center frequency . Function butter() is used to find the filter coefficients.A low pass filter is implemented.


%Program to design an IIR Butterworth filter


clear all;
close all;
fp=input('Enter the pass band frequency fp   = ');
fs=input('Enter the stop band frequency fs   = ');
rp=input('Enter the pass band attenuation rp = ');
rs=input('Enter the stop band attenuation rs = ');
f=input ('Enter the sampling frequency f     = ');


wp=2*fp/f;
ws=2*fs/f;


[n,wn]=buttord(wp,ws,rp,rs);


[b,a]=butter(n,wn,'low');


freqz(b,a,500,f);
TITLE ('Magnitude and phase respose of the IIR butterworth filter');


%output
%Enter the pass band frequency fp   = 1000
%Enter the stop band frequency fs   = 1200
%Enter the pass band attenuation rp = .2
%Enter the stop band attenuation rs = 45
%Enter the sampling frequency f     = 3000


MATLAB Program for FIR Filter Using Window

Popular window coefficients.
  1. hann() - for hanning window
  2. hamming() - for hamming window
  3. blackman() - for blackman window
  4. kaiser() - for kaiser window
% Program to design a FIR filter using windows.

close all;
clear all;

fp=input('Enter the pass band frequency');
fs=input('Enter the stop band frequency');
rp=input(' Enter the pass band attenuation');
rs=input('Enter the stop band attenuation');
f=input(' Enter the sampling frequency');

%Calculating filter order

num=-20*log10(sqrt(rp*rs))-13;
dem=14.6*(fs-fp)/f;
n=ceil(num/dem);
n=abs(n);

% Normalizing the frequencies

wp=2*fp/f;
ws=2*fs/f;
wn=(ws+wp)/2;

%Adjusting the filter order. The order of window must be an odd number 
%and the order of filter must be one less than that of the window 

if (rem(n,2)==0)
    m=n+1;
else
        m=n;
        n=n-1;
end

%Window sequence calculation

w=hann(m);

%Calculation of filter coefficients

b=fir1(n,wn,'low',w);

%Plotting the filter response

freqz(b,1,500,3000);
TITLE('Magnitude and Phase response');


Output:
%output
%Enter the pass band frequency1000
%Enter the stop band frequency1200
%Enter the pass band attenuation.2
%Enter the stop band attenuation45
%Enter the sampling frequency3000


You can change this lowpass filter to high pass filter by changing the option 'low' to 
'high' in the fir1() function. The output is shown below.



%output
%Enter the pass band frequency1200
%Enter the stop band frequency1000
%Enter the pass band attenuation.2
%Enter the stop band attenuation45
%Enter the sampling frequency3000