Matlab Programs

26
%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 Program to generate a square with the required amplitude, duty cycle and frequency. square() function is used to generate the square wave.

Transcript of Matlab Programs

Page 1: Matlab Programs

%program to generate an exponentially damped sinusoidal waveclose 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

Program to generate a square with the required amplitude, duty cycle and frequency. square() function is used to generate the square wave.

Page 2: Matlab Programs

%Program to generate a continuous time square wave

close all;clear all;a=input('Enter the amplitude of the square wave A = ');f= input('Enter the frequency of the square wave F = ');dc=input('Enter the duty cycle of the wave DC = ');f=f*2*pi;t=0:.001:1;y=a*square(f*t,dc);plot(t,y);axis([0 1 -2.2 2.2]);

%output

%Enter the amplitude of the square wave A = 2%Enter the frequency of the square wave F = 10%Enter the duty cycle of the wave DC = 50

%Program to generate a discrete time square wave

close all;clear all;

a=input('Enter the amplitude of the square wave A = ');f= input('Enter the frequency of the square wave F = ');dc=input('Enter the duty cycle of the wave DC = ');f=f*2*pi;t=-10:.01:10;y=a*square(f*t,dc);stem(t,y);

Page 3: Matlab Programs

axis([0 1 -2.2 2.2]);

%output

%Enter the amplitude of the square wave A = 2%Enter the frequency of the square wave F = 8%Enter the duty cycle of the wave DC = 50

Program for the design of the IIR filter is given below. 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);

Page 4: Matlab Programs

[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

Program for the design of the IIR filter is given below. 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;

Page 5: Matlab Programs

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

%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     = ');

Page 6: Matlab Programs

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

% 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

Page 7: Matlab Programs

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%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

Page 8: Matlab Programs

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

% Program to design FIR bandpass filter .

close all;clear all;

Page 9: Matlab Programs

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 frequency5000%Enter the order of the filter50

%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);

Page 10: Matlab Programs

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

Page 11: Matlab Programs

MATLAB Programs 07: Finding the DFT and IDFT of a sequence without using inbuilt functions

5:06 AM Anil C S 7 comments Email This BlogThis! Share to Twitter Share to Facebook

THEORY

A little basics will aid to get a better understanding of the program 

Basic eqation to find the DFT of a sequence is given below.

Basic equation to find the IDFT of a sequence is given below.  

It is based on these two equations the program is written.

PROGRAM

%program to find the DFT/IDFT of a sequence without using the inbuilt functions

close all;clear all;xn=input('Enter the sequence x(n)'); %Get the sequence from user ln=length(xn); %find the length of the sequencexk=zeros(1,ln); %initilise an array of same size as that of input sequenceixk=zeros(1,ln); %initilise an array of same size as that of input sequence

%code block to find the DFT of the sequence

%-----------------------------------------------------------for k=0:ln-1    for n=0:ln-1

Page 12: Matlab Programs

        xk(k+1)=xk(k+1)+(xn(n+1)*exp((-i)*2*pi*k*n/ln));    endend%------------------------------------------------------------

%code block to plot the input sequence

%------------------------------------------------------------t=0:ln-1;subplot(221);stem(t,xn);ylabel ('Amplitude');xlabel ('Time Index');TITLE ('Input Sequence');%---------------------------------------------------------------

magnitude=abs(xk); % Find the magnitudes of individual DFT points

%code block to plot the magnitude response

%------------------------------------------------------------t=0:ln-1;subplot(222);stem(t,magnitude);ylabel ('Amplitude');xlabel ('K');TITLE ('Magnitude Response');%------------------------------------------------------------

phase=angle(xk); % Find the phases of individual DFT points

%code block to plot the magnitude sequence

%------------------------------------------------------------t=0:ln-1;subplot(223);stem(t,phase);ylabel ('Phase');xlabel ('K');TITLE ('Phase Response');%------------------------------------------------------------

Page 13: Matlab Programs

% Code block to find the IDFT of the sequence

%------------------------------------------------------------for n=0:ln-1    for k=0:ln-1        ixk(n+1)=ixk(n+1)+(xk(k+1)*exp(i*2*pi*k*n/ln));    endend

ixk=ixk./ln;%------------------------------------------------------------

%code block to plot the input sequence

%------------------------------------------------------------t=0:ln-1;subplot(224);stem(t,xn);ylabel ('Amplitude');xlabel ('Time Index');TITLE ('IDFT sequence');%-----------------------------------------------------------

Page 14: Matlab Programs

%Program to generate triangular waveform

n=input ('Enter the length of the sequence N= ');t=0:n;y=(-1).^t; plot(t,y);ylabel ('Amplitude');xlabel ('Time Index');TITLE ('Triangular waveform');

In the above program the concept of ramp signal is used. The slope of the ramp is changed alternatively to generate a triangular waveform. If two successive ramps are considered then one will be a positive going ramp and other will be a negative going ramp. Y signal i.e. the amplitude is expressed as the product of time index 't' and  power of -1 raised to time index. Thus for even time index the slope will be positive and for odd time index slope will be negative. Thus a triangular waveform is generated.

Page 15: Matlab Programs

Now replace plot() with stem() and observe the result. You will not get the expected wave form. Think about the reason for it.

%Program to generate sawtooth waveform

n=input ('Enter the length of the sequence N= ');t=0:.0001:n;y=sawtooth(t);plot(t,y);ylabel ('Amplitude');xlabel ('Time Index');TITLE ('Sawtooth waveform');

Page 16: Matlab Programs

Now to generate a triangular waveform we need only to change the duty cycle of the sawtooth waveform to 50% as shown in the program given below. 

%Program to generate triangular waveform

n=input ('Enter the length of the sequence N= ');t=0:.0001:n;y=sawtooth(t,.5); %sawtooth with 50% duty cycle (triangular) plot(t,y);ylabel ('Amplitude');xlabel ('Time Index');TITLE ('Triangular waveform');

Page 17: Matlab Programs

%Program to generate the exponential sequence

n=input('Enter the duration of the signal N = ');a=input ('Enter the scaling factor a = ');t=0:.1:n-1;y=exp(a*t);plot(t,y);ylabel ('Amplitude');xlabel ('Time Index');TITLE ('Exponential Signal');

Page 18: Matlab Programs

Notice the difference by replacing plot() with stem().

%program to generate Impulse Signal

t=-2:2;y=zeros(1,5);y(1,3)=1;stem(t,y);ylabel ('Amplitude');xlabel ('Time Index');TITLE ('Impulse Signal');

Here we have created an array Y with the following data 0,0,1,0,0. When this is compared to the time axis we get '1' for the time zero which represents the impulse signal.

Page 19: Matlab Programs

Try plot(), instead of stem(). You may not get the required result. Guess the reason for it !!!. If you could not make it out ask me.

% Program to generate a ramp signal

n=input('Enter the length of ramp sequence N = ');  % Get the lengths=input(' Enter the slop of the ramp S = ');  % Get the slop of the rampt=0:n-1;plot(t,s*t);ylabel ('Amplitude');xlabel ('Time  Index');TITLE ('Ramp signal');

Page 20: Matlab Programs

This time you may try yourself by replacing plot() with stem();

% Program to generate Unit Step sequence 

n=input ('Enter the length of the step sequence N='); % Get the length of the require sequence from the usert=0:n-1; % defines the time axisy=ones(1,n); % defines an 1 x n matrix which is filled with onesstem(t,y); %displays the data as linesylabel ('Amplitude'); % name the Y axisxlabel ('Time Index'); %Name the x axisTITLE ('Unit Step Signal'); % Giving the title for the plot

Page 21: Matlab Programs

% Program to generate Unit Step signal 

n=input ('Enter the lenght of the step sequence N='); % Get the length of the require sequence from the usert=0:n-1;                                              % defines the time axisy=ones(1,n);                                          % defines an 1 x n matrix which is filled with oneplot(t,y);                                            % Plot the graphylabel ('Amplitude');                                 % name the Y axisxlabel ('Time Index');                                %Name the x axisTITLE ('Unit Step Signal');                           % Giving the title for the plot

Page 22: Matlab Programs

By this time you might have understood the difference between stem() and plot().

MATLAB code for n-channel enhancement MOSFET with early effect

Enhancement mode MOSFET is influenced by early effect that arises in short channel devices. Since we are dealing with 180nm technology the devices are prone to short channel effects. This channel length modulation introduces an additional term in the MOSFET equation. It’s represented as ‘λ’ but is different from the device parameter used for expressing sizes of transistor parts. It’s the inverse of early voltage. The implication can be understood by observing the output characteristics. The curve influence by channel length modulation will develop a slope corresponding to the early voltage in the saturation region of the output characteristic of the NMOSFET. This video gives a better explanation to the working of MOSFET and guides you through the implementation of code and interpretation of graphs.

%n-channel enhancement mode MOSFET output characteristics clear all;%kn=un*cox = 100 microA/Voltskn=1e-4;%Let W/L= 2W=360*(10^(-9));L=180*(10^(-9));beta=kn*W/L;%Vth is the threshold voltageVth=1;

Page 23: Matlab Programs

%lambda is the inverse of early voltage in voltage inverselambda=1/1000;%Sweep drain to source voltge from 0 to 10Vvds=0:0.5:10;%Ask the user to enter gate to source voltagevgs=input('ENTER THE Vgs in volts');%Estimate length of the arraym=length(vds);for i=1:m

if vgs < Vthcurrent(1,i)=0;current1(1,i)=0;elseif vds(i) >= (vgs - Vth)current(1,i)=0.5* beta * ((vgs - Vth)^2)*(1+lambda*vds(i));current1(1,i)=0.5* beta * ((vgs - Vth)^2);elseif vds(i) < (vgs - Vth)current(1,i)= beta*((vgs-Vth)*vds(i) - 0.5*(vds(i)^2))*(1+lambda*vds(i));current1(1,i)=beta*((vgs-Vth)*vds(i) - 0.5*(vds(i)^2));end

endplot(vds,current(1,:),'b',vds,current1(1,:),'r')xlabel('Vds, V')ylabel('Drain Current,A')title('I-V Characteristics of a MOSFET')

OUTPUT CHARACTERISTICSIn this graph, the early voltage is 200V. So the effect is higher than the case following this. The output characteristics have been highly modified with the help og graph editor in MATLAB to help viewers explore them.

Page 24: Matlab Programs

 In this graph the early voltage is 1000 V. Therefore, the curve is not so sloppy. This is the original appearance of the output curve without any modification. In other words, this curve obeys the code!