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.”