MATLAB program for normal distribution

Program Code
% Normal Distribution
clc;
close all;
clear all;
x=input('Enter the input sequence ');
m=input('Enter the value of mean ');
s=input('Enter the standard deviation ');
n=length(x);
a=-(x-m).^2;
b=a/(2*s^2);
c=1/(s*((2*pi)^(1/2)));
d=exp(b);
y=c*d;
t=0:n-1;
plot(t,y);
title('Normal Distribution');
grid on;




Example of Output
Enter the input sequence [125 135 145 155 165 175 185 195]
Enter the value of mean 165.5

Enter the standard deviation 15.26



_______________________

MATLAB program for sampling rate conversion

Program Code
%sampling rate conversion
clc;
close all;
clear all;
N=input('Enter the length of the sequence N= ');
n=0:N-1;
f1=input('Enter the first frequency component of the signal f1= ');
f2=input('Enter the second frequency component of the signal f2= ');
x=sin(2*pi*f1*n)+sin(2*pi*f2*n);
L=input('Input the up sampling factor L= ');
x1=zeros(1,L*N);
n1=1:L*N;
a=1:L:L*N;
x1(a)=x;
M=input('Input the down sampling rate M= ');
x2=x1(1:M:(L*N));
n2=1:((L*N)/M);
subplot(311);
stem(n,x);
xlabel('discrete frequency');
ylabel('Amplitude');
title('input sequence');
grid on;
subplot(312);
stem(n1,x1);
xlabel('discrete frequency');
ylabel('Amplitude');
title('up sampled sequence');
grid on;
subplot(313);
stem(n2,x2);
xlabel('discrete frequency');
ylabel('Amplitude');
title('down sampled sequence');
grid on;



Example of Output
Enter the length of the sequence N= 100
Enter the first frequency component of the signal f1= 1000
Enter the second frequency component of the signal f2= 250
Input the up sampling factor L= 3
Input the down sampling rate M= 2



____________________________

MATLAB program for equiripple FIR filter

Program Code
%equiripple FIR filter
clc;
close all;
clear all;
Fs=1000;
Fp=input('Input the pass band frequency Fp= ');
Fst=input('Input the stop band frequency Fst= ');
Ap=input('Input the pass band attenuation Ap= ');
Ast=input('Input the stop band attenuation Ast= ');
d=fdesign.lowpass('Fp,Fst,Ap,Ast');
Hd=design(d,'equiripple');
fvtool(Hd);




Example of Output
Input the pass band frequency Fp= 16000
Input the stop band frequency Fst= 12000
Input the pass band attenuation Ap= 1
Input the stop band attenuation Ast= 50



__________________________________

MATLAB program for Ideal Low Pass Linear Phase Fir Filter

Program Code
clc;
close all;
clear all;
wc=input('Input the cut-off frequency in radians(less than pi)');
M=input('Input the length of ideal filter');
if wc>pi
    error('cut-off frequency should be less than pi')
    return
end
alpha=(M-1)/2;
n=0:1:(M-1);
m=n-alpha+eps;
hd=sin(wc*m)./(pi*m);
if nargout==0
    stem(n,hd);
    title('Impulse response of ideal low pass filter');
    xlabel('n');
    ylabel('hd(n)');
end





                      Example of Output
Input the cut-off frequency in radians(less than pi)    3
Input the length of ideal filter     50

ans =

  Columns 1 through 7 

   -0.0123    0.0133   -0.0141    0.0147   -0.0151    0.0152   -0.0149

  Columns 8 through 14 

    0.0143   -0.0134    0.0120   -0.0102    0.0079   -0.0050    0.0016

  Columns 15 through 21 

    0.0025   -0.0075    0.0134   -0.0207    0.0297   -0.0412    0.0569

  Columns 22 through 28 

   -0.0800    0.1194   -0.2074    0.6350    0.6350   -0.2074    0.1194

  Columns 29 through 35 

   -0.0800    0.0569   -0.0412    0.0297   -0.0207    0.0134   -0.0075

  Columns 36 through 42 

    0.0025    0.0016   -0.0050    0.0079   -0.0102    0.0120   -0.0134

  Columns 43 through 49 

    0.0143   -0.0149    0.0152   -0.0151    0.0147   -0.0141    0.0133

  Column 50 


   -0.0123

___________________________

MATLAB program for unfolding and unwrapping of DFT

Program Code
%Unfolding and Unwrapping of DFT
clc;
close all;
clear all;
x=input('Enter the sequence x= ');
N=input('Enter the length of the DFT N= ');
len=length(x);
if N>len
    x=[x zeros(1,N-len)];
elseif N<len
    x=x(1:N);
end
i=sqrt(-1);
w=exp(-i*2*pi/N);
n=0:(N-1);
k=0:(N-1);
nk=n'*k;
W=w.^nk;
X=x*W;
disp(X);
subplot(411);
stem(k,abs(X));
title('Magnitude plot of unwrapped sequence ');
subplot(412);
stem(k,angle(X));
title('Phase plot ofunwrapped sequence ');
y=X(floor(N/2)+1:N);
Y=[y X(1:floor(N/2))];
subplot(413);
stem(k,abs(Y));
title('Magnitude plot of unfolded spectrum ');
subplot(414);
stem(k,angle(Y));
title('Phase plot of unfolded spectrum ');



Example of Output
Enter the sequence  x= [1 1 2 2 3 3 2 2 1 1]
Enter the length of the DFT  N= 69
  Columns 1 through 4 

  18.0000            16.1310 - 7.0067i  11.1882 -11.9796i   4.8688 -13.6995i

  Columns 5 through 8 

  -0.8338 -12.1898i  -4.4438 - 8.5762i  -5.5078 - 4.4810i  -4.5846 - 1.2845i

  Columns 9 through 12 

  -2.7880 + 0.3832i  -1.1687 + 0.7107i  -0.2635 + 0.3733i  -0.0093 + 0.0449i

  Columns 13 through 16 

   0.0082 + 0.0394i   0.1772 + 0.2510i   0.5978 + 0.3635i   1.0701 + 0.1471i

  Columns 17 through 20 

   1.2925 - 0.3622i   1.1048 - 0.8989i   0.6049 - 1.1673i   0.0716 - 1.0466i

  Columns 21 through 24 

  -0.2337 - 0.6577i  -0.2394 - 0.2563i  -0.0873 - 0.0379i   0.0000 + 0.0000i

  Columns 25 through 28 

  -0.0920 + 0.0400i  -0.2666 + 0.2855i  -0.2763 + 0.7775i   0.0905 + 1.3226i

  Columns 29 through 32 

   0.8264 + 1.5948i   1.6598 + 1.3503i   2.1917 + 0.6141i   2.1373 - 0.2938i

  Columns 33 through 36 

   1.5176 - 0.9229i   0.6738 - 0.9545i   0.0829 - 0.3990i   0.0829 + 0.3990i

  Columns 37 through 40 

   0.6738 + 0.9545i   1.5176 + 0.9229i   2.1373 + 0.2938i   2.1917 - 0.6141i

  Columns 41 through 44 

   1.6598 - 1.3503i   0.8264 - 1.5948i   0.0905 - 1.3226i  -0.2763 - 0.7775i

  Columns 45 through 48 

  -0.2666 - 0.2855i  -0.0920 - 0.0400i  -0.0000 + 0.0000i  -0.0873 + 0.0379i

  Columns 49 through 52 

  -0.2394 + 0.2563i  -0.2337 + 0.6577i   0.0716 + 1.0466i   0.6049 + 1.1673i

  Columns 53 through 56 

   1.1048 + 0.8989i   1.2925 + 0.3622i   1.0701 - 0.1471i   0.5978 - 0.3635i

  Columns 57 through 60 

   0.1772 - 0.2510i   0.0082 - 0.0394i  -0.0093 - 0.0449i  -0.2635 - 0.3733i

  Columns 61 through 64 

  -1.1687 - 0.7107i  -2.7880 - 0.3832i  -4.5846 + 1.2845i  -5.5078 + 4.4810i

  Columns 65 through 68 

  -4.4438 + 8.5762i  -0.8338 +12.1898i   4.8688 +13.6995i  11.1882 +11.9796i

  Column 69 


  16.1310 + 7.0067i



______________________________

MATLAB program to find DFT and IDFT using matlab functions

Program Code
%DFT and IDFT using matlab functions
clc;
close all;
clear all;
x=input('Please enter the sequence x(n)=');
N=input('Please enter the length of the DFT N=');
X=fft(x,N);
n=0:length(x)-1;
subplot(311);
stem(n,x);
title('Input Sequence');
subplot(323);
n=0:length(X)-1;
stem(n,X);
disp('DFT of input sequence is ');
disp(X);
title('DFT');
subplot(324);
stem(n,abs(X));
title('Magnitude spectrum');
subplot(325);
stem(n,angle(X));
title('Phase spectrum');
xr=ifft(x,N);
subplot(326);
stem(n,abs(xr));
title('IDFT');
disp('IDFT of input sequence is ');
disp(xr);
Example of Output
Please enter the sequence x(n)=[1 2 3 4 5 6 7 8 9]
Please enter the length of the DFT N=6
DFT of input sequence is 
  Columns 1 through 4

  21.0000 + 0.0000i      -3.0000 + 5.1962i       -3.0000 + 1.7321i       -3.0000 + 0.0000i

  Columns 5 through 6

  -3.0000 - 1.7321i          -3.0000 - 5.1962i

IDFT of input sequence is 
  Columns 1 through 4

   3.5000 + 0.0000i       -0.5000 - 0.8660i         -0.5000 - 0.2887i       -0.5000 + 0.0000i

  Columns 5 through 6

  -0.5000 + 0.2887i         -0.5000 + 0.8660i


__________________________________



Advertisement 



MATLAB program to find IDFT without using MATLAB function

Program Code
%IDFT program without function
clc;
close all;
clear all;
X=input('Enter the sequence');
N=input('Enter the length of the IDFT');
len=length(X);
if N>len
    X=[X zeros(1,N-len)];
elseif N<len
    X=X(1:N);
end
i=sqrt(-1);
w=exp(-i*2*pi/N);
n=0:(N-1);
k=0:(N-1);
nk=n'*k;
W=w.^(-nk);
x=(X*W)/N;
disp(x);
subplot(211);
stem(k,abs(x));
title('Magnitude Plot');
xlabel('N');
ylabel('Amplitude');
grid on;
subplot(212);
stem(k,angle(x));
title('Phase Plot');
xlabel('N');
ylabel('Phase Angle');
grid on;


Example of Output
Enter the sequence     [24.0000       -2.3264 -13.6637i             3.0930 + 4.7651i        1.2334 - 6.2528i        1.2334 + 6.2528i 3.0930 - 4.7651i        -2.3264 +13.6637i]

Enter the length of the IDFT    4

6.1917 - 2.2247i         7.1913 + 2.0611i       5.8083 - 4.6072i   4.8087 + 4.7708i




_________________________

MATLAB program to find DFT without using Matlab function

Progam Code
% DFT program without function
clc;
close all;
clear all;
x=input('Enter the sequence x= ');
N=input('Enter the length of the DFT N= ');
len=length(x);
if N>len
    x=[x zeros(1,N-len)];
elseif N<len
    x=x(1:N);
end
i=sqrt(-1);
w=exp(-i*2*pi/N);
n=0:(N-1);
k=0:(N-1);
nk=n'*k;
W=w.^nk;
X=x*W;
disp(X);
subplot(211);
stem(k,abs(X));
title('Magnitude Spectrum');
xlabel('Discrete frequency');
ylabel('Amplitude');
grid on;
subplot(212);
stem(k,angle(X));
title('Phase Spectrum');
xlabel('Discrete frequency');
ylabel('Phase Angle');
grid on;



Example of output

Enter the sequence x= [4 5 6 9]
Enter the length of the DFT N= 7
  Columns 1 through 4 

  24.0000            -2.3264 -13.6637i                    3.0930 + 4.7651i   
1.2334 - 6.2528i

  Columns 5 through 7 


 1.2334 + 6.2528i            3.0930 - 4.7651i            -2.3264 +13.6637i


____________________________





Advertisement


MATLAB program for PWM signal generation

Program Code
% PWM Signal Generation
clc;
close all;
clear all;
t=0:0.001:1;
s=sawtooth(2*pi*10*t+pi);
m=0.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,'-g',t,m,'--r',t,s,'--b');
grid on;
ylabel('Amplitude');
xlabel('Time index');
title('PWM Wave');
axis([0 1 -1.5 1.5]);



Output

_________________

MATLAB program for FM signal generation

Program Code
% FM signal generation
clc;
close all;
clear all;
fc=input('Please enter the carrier signal frequency in Hz,fc=');
fm=input('Please enter the modulating signal frequency in Hz,fm=');
m=input('Modulation index,m=');
n=0:0.0001:.1;
c=sin(2*pi*fc*n);
M=sin(2*pi*fm*n);
subplot(311);
plot (n,c);
ylabel('Amplitude');
xlabel('Time index');
title('Carrier signal ');
subplot(312);
plot (n,M);
ylabel('Amplitude');
xlabel('Time index');
title('Modulating Signal');
y=sin(2*pi*fc*n+(m.*sin(2*pi*fm*n)));
subplot(313);
plot (n,y);
ylabel('Amplitude');
xlabel('Time index');
title('Frequency Modulated signal');



Example of Output
Please enter the carrier signal frequency in Hz,fc=1000
Please enter the modulating signal frequency in Hz,fm=250

Modulation index,m=2


____________________________

MATLAB program for AM signal generation

Program Code
clc;
close all;
clear all;
% AM signal generation
fc=input('Please enter the carrier signal frequency in Hz,fc=');
fm=input('Please enter the modulating signal frequency in Hz,fm=');
m=input('Modulation index,m=');
n=0:0.001:1;
c=sin(2*pi*fc*n);
M=sin(2*pi*fm*n);
y=(1+m*M).*c;
subplot(211);
plot (n,y);
ylabel('Amplitude');
xlabel('Time index');
title('Amplitude Modulated signals');
y1=M.*c;
subplot(212);
plot(n,y1);
ylabel('Amplitude');
xlabel('Time index');
title('supressed Carrier');




Example of Output
Please enter the carrier signal frequency in Hz,fc=50
Please enter the modulating signal frequency in Hz,fm=5
Modulation index,m=.5

__________________________

MATLAB program for exponential waveform generation

Program Code
n=0:0.01:5;
a=2;
y=exp(-a*n);
plot (n,y);
xlabel ('Time');
ylabel ('Amplitude');
title ('Exponential waveform');
grid on;



Output

________________________

MATLAB program for step waveform generation

Program Code
n=0:5;
y=[ones(1,6)];
stem (n,y);
xlabel ('Time');
ylabel ('Amplitude');
title ('Step Waveform');
grid on;



Output

Explanation of Program Code
clc;
It clears all input and output from the Command Window display giving clean screen. It removes items from workspace, freeing up system memory. After using clc, the scroll bar cannot be used to see the history of functions, but still the up arrow can be used to recall statements from the command history.

clear all;
It removes all variables from the workspace. This frees up system memory.

close all;
It deletes all figures whose handles are not hidden.

n=0:5;
This command creates vector n.
Vector n is given by
n= [0     1       2            3        4        5]
Here vector n is used as x axis.

y=[ones(1,6)];
This command generates step waveform.

stem (n,y);
This command plots n versus the columns of y as stems. . n and y must be vectors or matrices of the same size. Additionally, n can be a row or a column vector and y a matrix with length(n) rows.
This command makes step waveform appear on the screen.

xlabel ('Time');
It labels the x-axis as 'Time'. Each axes graphics object can have one label for the x-, y-, and z-axis. The label appears beneath its respective axis in a two-dimensional plot.

ylabel ('Amplitude');
It labels the y-axis as ' Amplitude. '

title ('Step Waveform');
It outputs the phrase  'Step Waveform' above the figure at the top.

grid on;
It adds major grid lines to the current axes.
______________

MATLAB program for impulse waveform generation

Program Code
n=-5:5;
y=[zeros(1,5) 1 zeros(1,5)];
stem (n,y);
xlabel ('Time');
ylabel ('Amplitude');
title ('Impulse Waveform');
grid on;



Output

Explanation of Program Code 
clc;
It clears all input and output from the Command Window display giving clean screen. It removes items from workspace, freeing up system memory. After using clc, the scroll bar cannot be used to see the history of functions, but still the up arrow can be used to recall statements from the command history.

clear all;
It removes all variables from the workspace. This frees up system memory.

close all;
It deletes all figures whose handles are not hidden.

n=-5:5;
This command creates vector n.
Vector n is given by
n= [-5    -4    -3    -2    -1     0     1       2       3        4        5]
Here vector n is used as x axis

y=[zeros(1,5) 1 zeros(1,5)];
This command creates impulse waveform.

stem (n,y);
This command plots n versus the columns of y as stems. . n and y must be vectors or matrices of the same size. Additionally, n can be a row or a column vector and y a matrix with length(n) rows.
This command makes impulse waveform appear on the screen.

xlabel ('Time');
It labels the x-axis as 'Time'. Each axes graphics object can have one label for the x-, y-, and z-axis. The label appears beneath its respective axis in a two-dimensional plot.

ylabel ('Amplitude');
It labels the y-axis as ' Amplitude. '

title ('Impulse Waveform');
It outputs the phrase  'Impulse Waveform' above the figure at the top.

grid on;
It adds major grid lines to the current axes.
________________

MATLAB program for sawtooth waveform generation

Program Code
t=0:0.01:4;
y=sawtooth(2*pi*t+pi);
plot (t,y);
axis ([0 4 -5 5]);
xlabel ('Time');
ylabel ('Amplitude');
title ('Sawtooth Waveform');
grid on; 



Output


Explanation of Program Code 
clc;
It clears all input and output from the Command Window display giving clean screen. It removes items from workspace, freeing up system memory. After using clc, the scroll bar cannot be used to see the history of functions, but still the up arrow can be used to recall statements from the command history.

clear all;
It removes all variables from the workspace. This frees up system memory.

close all;
It deletes all figures whose handles are not hidden.

t=0:0.01:4;
Here vector t is created as time axis.
Vector t has 401 elements. The starting element is 0 and the final element is 4. There is an increment of 0.01 between consecutive elements of vector t.

y=sawtooth(2*pi*t+pi);
It creates the sawtooth waveform.

plot (t,y);
It plots all the lines defined by t versus y pairs. That is, it makes the sawtooth waveform appear on the screen.

axis ([0 4 -5 5]);
It sets the limits for the x- and y-axis of the current axes.
Here according to the above command x-axis begins at 0 and ends at 4. Here y-axis begins at -5 and ends at 5.

xlabel ('Time');
It labels the x-axis as 'Time'. Each axes graphics object can have one label for the x-, y-, and z-axis. The label appears beneath its respective axis in a two-dimensional plot.

ylabel ('Amplitude');
It labels the y-axis as ' Amplitude. '

title ('Sawtooth Waveform');
It outputs the phrase  'Sawtooth Waveform' above the figure at the top.

grid on;
It adds major grid lines to the current axes.

_________________

MATLAB program for square wave generation

Program Code
t=0:0.01:4;
y=square(2*pi*t,50);
plot (t,y);
axis ([0 4 -2 2]);
xlabel ('Time');
ylabel ('Amplitude');
title ('Square Waveform');
grid on;



Output
Explanation of Program Code
 clc;
It clears all input and output from the Command Window display giving clean screen. It removes items from workspace, freeing up system memory. After using clc, the scroll bar cannot be used to see the history of functions, but still the up arrow can be used to recall statements from the command history.

clear all;
It removes all variables from the workspace. This frees up system memory.

close all;
It deletes all figures whose handles are not hidden.


t=0:0.01:4;
Here vector t is created as time axis. 
Vector t has 401 elements. The starting element is 0 and the final element is 4. There is an increment of 0.01 between consecutive elements of vector t.

y=square(2*pi*t,50);
This command creates a square waveform.

plot (t,y);
It plots all the lines defined by t versus y pairs. That is, it makes the square waveform appear on the screen.

axis ([0 4 -2 2]);
It sets the limits for the x- and y-axis of the current axes.
Here according to the above command x-axis begins at 0 and ends at 4. Here y-axis begins at -2 and ends at 2.

xlabel ('Time');
It labels x axis(time axis) as Time. Each axes graphics object can have one label for the x-, y-, and z-axis. The label appears beneath its respective axis in a two-dimensional plot.

ylabel ('Amplitude');
It labels y axis as amplitude.

title ('Square Waveform');
It outputs the phrase 'Square Waveform' above the figure at the top.

grid on;
It adds major grid lines to the current axes.
_________________

MATLAB program for Ramp Waveform generation

Program code
n=1:5;
stem (n,2*n);
xlabel ('N');
ylabel ('Amplitude');
title ('Ramp Waveform');
grid on; 



Output

Explanation of Program Code
clc;
It clears all input and output from the Command Window display giving clean screen. It removes items from workspace, freeing up system memory. After using clc, the scroll bar cannot be used to see the history of functions, but still the up arrow can be used to recall statements from the command history.

clear all;
It removes all variables from the workspace. This frees up system memory.

close all;
It deletes all figures whose handles are not hidden.

n=1:5;
Here the following vector n is created.
n=[1     2     3     4     5]
Here this vector n is used as time axis(x axis) while plotting ramp waveform.

stem (n,2*n);
It is used to create ramp waveform.

xlabel ('N');
It labels x axis(time axis) as N. Each axes graphics object can have one label for the x-, y-, and z-axis. The label appears beneath its respective axis in a two-dimensional plot.

ylabel ('Amplitude');
It labels y axis as amplitude.

title ('Ramp Waveform');
It outputs the phrase 'Ramp Waveform' above the figure at the top.

grid on;
It adds major grid lines to the current axes.
__________________

MATLAB Program to generate a cosine waveform

Program Code
clc;
clear all;
close all;
f=1000;
t=0:1/(f*1000):2/f;
y1=cos(2*pi*f*t);
plot (t,y1);
xlabel ('Time');
ylabel ('Amplitude');
title ('cosine Waveform');
grid on;



Output
_______________________

Matlab program to generate a sinusoidal waveform

Program Code
%sinusoidal waveform generation
clc;
clear all;
close all;
f=1000;
t=0:1/(f*1000):2/f;
y=sin(2*pi*f*t);
plot (t,y);
xlabel ('Time');
ylabel ('Amplitude');
title ('Sinusoidal Waveform');
grid on;




Output

Explanation of Program Code 
clc;
It clears all input and output from the Command Window display giving clean screen. It removes items from workspace, freeing up system memory. After using clc, the scroll bar cannot be used to see the history of functions, but still the up arrow can be used to recall statements from the command history.

clear all;
It removes all variables from the workspace. This frees up system memory.

close all;
It deletes all figures whose handles are not hidden.

f=1000;
It stores the value 1000 in variable f.

t=0:1/(f*1000):2/f;
This command creates a vector t.
The first element of vector t is 0. 
The next element of vector t is created by adding (1/1000f) to the previous element. This goes on by creating new elements till one element in the vector t has the value 2/f. The element with the value 2/f becomes the last element in vector t. 
Here f is already given as 1000. 
So here, 
1000f = 1000 X 1000 = 1000000
So, (1/1000f) = 1/1000000 = 0.000001
2/f = 2/1000=0.002
So, from the second element onward, each element of vector t is created by adding (1/1000f), that is 0.000001 with the previous element. This goes on by creating new elements till one element in the vector t has the value 2/f  that is 0.002. The element with the value 0.002 becomes the last element of the vector t.
Here this vector t is used as time axis(x axis) while plotting sinusoidal waveform.

y=sin(2*pi*f*t);
It is used to create sinusoidal waveform.
It multiplies each element of vector t with (2x 3.14x1000) and returns the circular sine of the elements of resulting vector.
Here f=1000. pi is always 3.14 approximately(constant).
That is why 2*pi*f*t = (2x 3.14x1000).

plot (t,y);
It plots all the lines defined by t versus y pairs.

xlabel ('Time');
It labels the x-axis as 'Time'. Each axes graphics object can have one label for the x-, y-, and z-axis. The label appears beneath its respective axis in a two-dimensional plot.

ylabel ('Amplitude');
It labels the y-axis as ' Amplitude. '

title ('Sinusoidal Waveform');
It outputs the phrase  'Sinusoidal Waveform' above the figure at the top.

grid on;
It adds major grid lines to the current axes.

__________________

My first post

  
Whenever you are unhappy or facing tough times in life
Don't worry ...

There will be light at the end of the tunnel
 and 
it will lead to greener pastures




Whenever there is happiness in your life,
Please think, “This will come again…”
Whenever there is sorrow in your life,
Please think, “This will change.”