Friday, August 8, 2008

Matlab - Week 2

Copy and paste the code below into a *.m file and then run the program.The file name must be saved with *.m extension , without spaces in between and no clashing of already existing functions in MATLAB Library


Example code for sinusoidal signals

%Generate Sine Wave
N=30;%Number of samples
t=-10:0.1:10;
x=sin(2*pi*25*t/N); %this is x(t)
plot(t,x)
title('Sine Wave');
xlabel('time scale');
ylabel('Amplitude');
axis([-70 70 -2 2]);
grid

Example code for square wave
%Square wave generation
A=2;
t = 0:0.0005:1;
x=A*square(2*pi*5*t,25); %5 Hertz wave with duty cycle 25%
plot(t,x);
grid
axis([0 1 -3 3]);

Example code for triangular wave
%Triangular wave generation
A=2;
t = 0:0.0005:1;
x=A*sawtooth(2*pi*5*t,0.25); %5 Hertz wave with duty cycle 25%
plot(t,x);
grid
axis([0 1 -3 3]);

Example code for rectangular pulse
%To generate a rectangular pulse
t=-5:0.01:5;
pulse = rectpuls(t,2); %pulse of width 2 time units
plot(t,pulse)
axis([-5 5 -1 2]);
grid

Example code for exponential signals
%Exponential signal
B=5;
alpha=5;
t=-1:0.1:1;
x=B*exp(-alpha*t);
plot(t,x);

%This part is for discrete signal
r=0.85;
n=-10:10;
y=B*r.^n;

stem(n,y);

Example code for decaying exponential signals
%Exponentially damped signal
A=10;
f=5; %5 Hertz
phi=0; %Phase info
a=6;
t=0:0.01:1;
x=A*sin(2*pi*f*t+phi).*exp(-a*t); %Elemental operation is required
plot(t,x);
grid

Elementary signals
%To generate a step signal
%Completely automatic
t=-2:0.01:2;
A=2;
len = length(t);
len_pos = length(find(t>=0));
u=A*([zeros(1,len-len_pos), ones(1,len_pos)]);
figure
plot(t,u);

axis([-2 4 0 3]);
grid

%Impulse function
%To generate impulse signal
delta = [zeros(1,(len-len_pos)+1), 1, zeros(1,len_pos-2)];
figure
plot(t,delta);

%Ramp function
%To generate ramp using step
u=A*([zeros(1,len-len_pos), ones(1,len_pos)]);
ramp = u.*t;

figure
plot(t,ramp);

Find Even and Odd signal
%Even and Odd signals
%Continuous time signal
t=-1:0.01:1;
len = length(t);
%Generate x(t)
x=sin(2*pi*t);
x_rev = sin(2*pi*(-t));

%Find Even and odd of x(t)
x_even = 0.5*(x + x_rev);
x_odd = 0.5*(x - x_rev);
plot(t,x,'r--',t,x_rev,'g-.',t,x_even,'m-',t,x_odd,'b*');
legend('x','x_rev','x_even', 'x_odd');
grid

Find the energy of the signal

%Find Energy

%To generate a rectangular pulse
t=-5:0.5:5;
pulse = 2*rectpuls(t,2.1); %pulse of width 2 time units
stem(t,pulse)
axis([-5 5 -1 2]);
grid

%Energy
Energy = 0;
t_mod = find(pulse > 0);
for i=t_mod(1):t_mod(length(t_mod))
Energy = Energy+ pulse(i).^2;
end
Energy


Operations that are checked are
1. Amplitude scaling
2. Addition
3. Multiplication
4. differentiation
5. Integration

Time scaling, reflection, time shifting

Example code to show all the operations on a exponentially damped signal
%Exponentially damped signal
A=10;
f=5; %5 Hertz
phi=0; %Phase info
a=6;
t=0:0.01:1;
x=A*sin(2*pi*f*t).*exp(-a*t); %Elemental operation is required
figure
plot(t,x);
grid

%Amplitude scaling
scale = 0.5;
y= scale * x;
figure
plot(t,x,'-',t,y,':');
grid

%Addition
figure
plot(t,x,'-',t,y,':',t,(x+y),'*');

%Multiplication
figure
plot(t,x,'-',t,y,':',t,(x.*y),'.');

%Time scaling
k=0.2;
scaled_time =t/k;
figure
plot(t,x,'g:',scaled_time, x);
grid

%Time Reflection
reflect_time = -t;
figure
plot(t,x,'g:',reflect_time, x);
grid

%Time shifting
shift = 1;
shifted_time = t-(-shift);
figure
plot(t,x,'g:',shifted_time, x);
grid

3 comments:

Anonymous said...

Good start for the blog. All the best. I have an question for you. I am electronics Engg. passed out in 1996 from India. Then worked in sales and moved to Canada. I want to go back to Electronics field and want to learn VHDL/ Verilog/ FPGA. I did worked in Marketing division of ASIC company back in 2003. Is this a good idea to start fresh at this point of time? I am very good at technical side. Pls. advise, THanks

Anonymous said...

HI, I was wondering, as per the google ad-sense policy, you can not have more then 3 ads in one page...i guess you have 4. you might want to double check it. Good Luck

Anonymous said...

Hello, can you please tell me, if you have any audio signal, and you want to write it as a new signal ranging between -1 to 1, i mean the amplitude be scaled between -1 to 1 how would you do it?