dsp assignment 1 and 2

๐Ÿงฉ Syntax:
WEEK 1
1. Generate different signals in continuous time, discrete time, plot and stem
respectively. Change time scale and write your observation.
CONTINUOUS TIME / PLOT FUNCTION:
clc;
clear all;
close all;
t = 0:0.001:1;
f = input('Enter the value of frequency');
a = input('Enter the value of amplitude');
subplot(3,3,1);
y = a*sin(2*pi*f*t);
plot(t,y,'r');
xlabel('time');
ylabel('amplitude');
title('Sine wave');
grid on;
subplot(3,3,2);
z = a*cos(2*pi*f*t);
plot(t,z);
xlabel('time');
ylabel('amplitude');
title('Cosine wave');
grid on;
subplot(3,3,3);
s = a*square(2*pi*f*t);
plot(t,s);
xlabel('time');
ylabel('amplitude');
title('Square wave');
grid on;
subplot(3,3,4);
plot(t,t);
xlabel('time');
ylabel('amplitude');
title('Ramp wave');
grid on;
subplot(3,3,5);
t1 = 0:1;
x = [t1 >= 0];
plot(t1,x,'r');
xlabel('time');
ylabel('amplitude');
title('Unit Step wave');
grid on;
SAMPLE INPUT:
Enter the value of frequency 2
Enter the value of amplitude 1
OUTPUT:
OBSERVATION:
Continuous Time Sine, Cosine, Square, Triangle and Unit step signals are plotted
using plot function.


2. DISCRETE TIME / STEM FUNCTION:
clc;
clear all;
close all;
n = 0:1:50 ;
f = input('Enter the value of frequency');
a = input('Enter the value of amplitude');
N = input('Enter the length of unit step');
subplot(3,3,1);
y = a*sin(2*pi*f*n);
stem(n,y,'r');
xlabel('time');
ylabel('amplitude');
title('Sine wave');
grid on;
subplot(3,3,2);
z = a*cos(2*pi*f*n);
stem(n,z);
xlabel('time');
ylabel('amplitude');
title('Cosine wave');
grid on;
subplot(3,3,3);
s = a*square(2*pi*f*n);
stem(n,s);
xlabel('time');
ylabel('amplitude');
title('Square wave');
grid on;
subplot(3,3,4);
stem(n,n);
xlabel('time');
ylabel('amplitude');
title('Ramp wave');
grid on;
subplot(3,3,5);
x = 0:N-1;
d = ones(1,N);
stem(x,d,'r');
xlabel('time');
ylabel('amplitude');
title('Unit Step wave');
grid on;
SAMPLE INPUT:
Enter the value of frequency 0.03
Enter the value of amplitude 1
Enter the length of unit step 9
OUTPUT:
OBSERVATION:
Discrete Time Sine, Cosine, Square, Triangle and Unit step signals are plotted
using stem function.
RESULT:
Continuous and Discrete time signals are generated in different forms for various
time scale.


                                          WEEK 2


1. Generate and plot the following signal X(n)=2ฮด(n+2) โ€“ ฮด(n-4), -5โ‰ค n โ‰ค 5
clc;
clear all;
close all;
n = -5:1:5;
a = (2*((n+2)==0)-((n-4)==0));
stem(n,a);
xlabel('Time');
ylabel('Amplitude');
title('IMPULSE');
grid on;
OUTPUT:


2. What are the results of these sets of commands?
A = zeros(1,5);
for n = 1:4
for m = 1:3
A = A + n*m;
end
end
disp(A)
RESULT:
60 60 60 60 60


3. Perform STEM of the following signals. Choose the range of n should be.
(1) f(n) = u(n) โˆ’ u(n โˆ’ 4)
(2) g(n) = n ยท u(n) โˆ’ 2 (n โˆ’ 4) ยท u(n โˆ’ 4) + (n โˆ’ 8) ยท u(n โˆ’ 8)
(3) x(n) = ฮด(n) โˆ’ 2 ฮด(n โˆ’ 4)
(4) y(n) = (0.9)n (u(n) โˆ’ u(n โˆ’ 20))
(5) v(n) = cos(0.12 ฯ€n) u(n)

clc;
clear all;
close all;
n = -2:1:22;
subplot(2,3,1);
f = (1*(n>=0))-(1*(n>=4));
stem(n,f);
xlabel('Time');
ylabel('Amplitude');
title('f[n]');
grid on;
subplot(2,3,2);
g = (n.*(n>=0))-(2.*(n-4).*(n>=4))+((n-8).*(n>=8));
stem(n,g,'r');
xlabel('Time');
ylabel('Amplitude');
title('g[n]');
grid on;
subplot(2,3,3);
x = (1*(n==0))-(2*(n==4));
stem(n,x,'g');
xlabel('Time');
ylabel('Amplitude');
title('x[n]');
grid on;
subplot(2,3,4);
y = (0.9.*n).*((n>=0)-(n>=20));
stem(n,y,'y');
xlabel('Time');
ylabel('Amplitude');
title('y[n]');
grid on;
subplot(2,3,5);
v = cos(0.12*pi*n).*(n>=0);
stem(n,v,'black');
xlabel('Time');
ylabel('Amplitude');
title('v[n]');
grid on;
OUTPUT:


4. Let f(n) = u(n) โˆ’ u(n โˆ’ 4)
g(n) = n ยท u(n) โˆ’ 2 (n โˆ’ 4) ยท u(n โˆ’ 4) + (n โˆ’ 8) ยท u(n โˆ’ 8).
 Use the CONV command to compute the convolutions.
(a) f(n) โˆ— f(n)
(b) f(n) โˆ— g(n)
(c) g(n) โˆ— ฮด(n)
(d) g(n) โˆ— g(n)
Comment on your observations: Do you see any relationship between f(n) โˆ—
f(n) and g(n) ? Compare f(n) with f(n) โˆ— f(n) and with f(n) โˆ— f(n) โˆ— f(n). What
happens as you repeatedly convolve this signal with itself?

clc;
clear all;
close all;
n = -2:1:10;
f = (1*(n>=0))-(1*(n>=4));
g = (n.*(n>=0))-(2.*(n-4).*(n>=4))+((n-8).*(n>=8));
subplot(2,3,1);
w = conv(f,f);
w_n = n(1)+n(1):1:length(w)+n(1)+n(1)-1;
stem(w_n,w);
xlabel('Time');
ylabel('Amplitude');
title('f[n]*f[n]')
grid on;
subplot(2,3,2);
x = conv(f,g);
x_n = n(1)+n(1):1:length(w)+n(1)+n(1)-1;
stem(x_n,x,'r');
xlabel('Time');
ylabel('Amplitude');
title('f[n]*g[n]')
grid on;
subplot(2,3,3);
y = conv(g,(n==0));
y_n = n(1)+n(1):1:length(w)+n(1)+n(1)-1;
stem(y_n,y,'g');
xlabel('Time');
ylabel('Amplitude');
title('g[n]*ฮด[n]')
grid on;
subplot(2,3,4);
z = conv(g,g);
z_n = n(1)+n(1):1:length(w)+n(1)+n(1)-1;
stem(z_n,z,'black');
xlabel('Time');
ylabel('Amplitude');
title('g[n]*g[n]')
grid on;
subplot(2,3,5);
a = conv(f,w);
a_n = n(1)+n(1)+n(1):1:length(a)+n(1)+n(1)+n(1)-1;
stem(a_n,a);
xlabel('Time');
ylabel('Amplitude');
title('f[n]*f[n]*f[n]')
grid on;
OUTPUT:
OBSERVATION:
When f(n) * f(n) and g(n) are compared it is observed that the signals are similar
with time shift. While f(n) is convoluted to itself repeatedly it is observed that
f(n) * f(n) * f(n) and f(n) * g(n) are similar with time shift.


5. Create a simple impulse response for an LTI system h(n) = ones (1,11). STEM
this response. Find output y(n) using CONV command where y(n) = x(n) *
h(n), Take x(n) to be unique of your choice (may be random, any electrical
signals). Plot y(n), x(n) and h(n). What is your observation from this operation?
Change h(n) and comment.

clc;
clear all;
close all;
xi = input('Enter the starting point of x[n] =');
x = input('Enter the co-efficients of x[n] =');
h = ones(1,11);
hi = 1;
x_n = xi:1:(xi+length(x)-1);
h_n = 1:1:11;
subplot(3,1,1);
stem(x_n,x);
grid on;
xlabel('Time');
ylabel('Amplitude');
title('INPUT x[n]');
subplot(3,1,2);
stem(h_n,h,'r');
grid on;
xlabel('Time');
ylabel('Amplitude');
title('IMPULSE RESPONSE h[n]');
subplot(3,1,3);
y = conv(x,h);
y_n = (xi+hi):1:(length(y)+xi+hi-1);
stem(y_n,y,'black');
grid on;
xlabel('Time');
ylabel('Amplitude');
title('CONVOLUTION y[n]');
SAMPLE INPUT:
Enter the starting point of x[n] = 0
Enter the co-efficients of x[n] = [1 2 3]
OUTPUT:


6. If y(n) = x(n) + 2 x(n โˆ’ 1) โˆ’ 0.95 y(n โˆ’ 1) is difference equation of the system.
Write your own Matlab function, โ€œdiffeqโ€ to implement this difference
equation using a for loop. Assume the input signal is N-samples long (0 โ‰ค n โ‰ค
N โˆ’ 1), your program should find the first N sample of the output y(n) (0 โ‰ค n โ‰ค
N โˆ’ 1). (Hint matlab indexing starts from 1). Use your Matlab function to
compute y1 = diffeq(x1): x1=1/2 *[u(n) โˆ’ u(n โˆ’ 10)].

clc;
clear all;
close all;
% y[n] = x[n] + 2 * x[n-1] - 0.95 * y[n-1]
% x[n] = 0.5 * [ u[n] - u[n-10] ]
% y[-1] is not given in question and x[-1] = 0
% y[0] = x[0] + 2 * x[-1] - 0.95 * y[-1]
% Hence
% y[-1] = 0 and y[0] = x[0] is considered
N = input('Enter the number of samples = ');
n = 0:1:N-1;
x = (0.5 * ((n>=0) - (n>=10)));
y = diffeq(x);
stem(n,y,'r');
grid on;
title('DIFFERENCE EQUATION');
xlabel('Time');
ylabel('Amplitude');
function y1 = diffeq(x1)
 y1 = zeros(1,length(x1));
 for i = 1:1:(length(x1))
 if i == 1
 y1(i) = x1(i);
 else
 y1(i) = x1(i) + 2 * x1(i-1) - 0.95 * y1(i-1);
 end
 end
end
SAMPLE INPUT:
Enter the number of samples = 15
OUTPUT:


7. Sketch the graphs (STEM) produced by the following code. I.
n = 0:10;
x = (n >= 1) - (n >=5);
stem(n,x)
h = (n == 5);
y = conv(h,x);
stem(0:20,y)
z = conv(x,x);
stem(0:20,z)


8. Write a Matlab code that will plot a sinusoid of frequency 50 Hz for 5 cycles.
Change the frequency of the sine wave from the previous section to 440 Hz
and plot the signal for the interval [โˆ’1, 1]. If you Plot the samples that lie in
the interval [0, 0.01] instead, what is your observation.

clc;
clear all;
close all;
f1 = 50;
f2 = 440;
if (-1+((1/f1)*5)) < 1
 t1 = -1:0.0001:(-1+((1/f1)*5));
 y1 = sin(2*pi*f1*t1);

 t2 = (-1+((1/f1)*5)):0.0001:1;
 y2 = sin(2*pi*f2*t2);
end
figure(1);
y = [y1,y2];
t = [t1,t2];
plot(t,y)
grid on;
title('SINE WAVE IN TIME INTERVAL [-1,1]');
xlabel('Time');
ylabel('Amplitude');
if (0+((1/f1)*5)) < 0.01
 t3 = 0:0.0001:(0+((1/f1)*5));
 y3 = sin(2*pi*f1*t3);

 t4 = (0+((1/f1)*5)):0.0001:0.01;
 y4 = sin(2*pi*f2*t4);
 t5 = [];
 y5 = [];
else
 t3 = [];
 y3 = [];

 t4 = [];
 y4 = [];
 t5 = 0:0.0001:0.01;
 y5 = sin(2*pi*f1*t5);
end
figure(2);
y_ = [y3,y4,y5];
t_ = [t3,t4,t5];
plot(t_,y_,'r')
grid on;
title('SINE WAVE IN TIME INTERVAL [0,0.01]');
xlabel('Time');
ylabel('Amplitude');
OUTPUT:
Continues till 1


9. Form an array of length 28, with zeros everywhere except in the 14 th position,
which has value 1. This array gives a discrete-time impulse, which is a signal
that is zero everywhere except at one sample point. Plot the impulse signal,
using both plot and stem.

clc;
clear all;
close all;
x = zeros(1,28);
x(14) = 1;
n = 1:1:28;
subplot(1,2,1);
plot(n,x);
grid on;
title('Continuous-Time-Plot');
xlabel('Time');
ylabel('Amplitude');
subplot(1,2,2);
stem(n,x,'g');
grid on;
title('Discrete-Time-Plot');
xlabel('Time');
ylabel('Amplitude');
OBSERVATOIN:
When the continuous and discrete time signal of impulse signal is observed we
can see that in continuous time signal the change is not instantaneous while in
discrete time signal an instantaneous pulse is generated.
OUTPUT:


10. Write a MATLAB program to perform following operation on signals (choose
suitable signals of your choice) for atleast length of not less than 10
(a) add x1(n) and x2(n),
(b) y(n)=prod(x1,x2), where prod is function to multiply.
(c) y(n)=x1(n)*x1(-n)
(d) y(n)=x1(2n)


clc;
clear all;
close all;
n = 0:1:15;
x1 = (1*(n>=3)-1*(n>=9));
x2 = (1*(n==2)+1*(n>=5)+1*(n>=8)-2*(n>=12));
subplot(2,3,1);
stem(n,x1,'r');
grid on;
title('x1[n]');
xlabel('Time');
ylabel('Amplitude');
subplot(2,3,2);
stem(n,x2,'g');
grid on;
title('x2[n]');
xlabel('Time');
ylabel('Amplitude');
subplot(2,3,3);
y = x1 + x2;
stem(n,y,'black');
grid on;
title('x1[n]+x2[n]');
xlabel('Time');
ylabel('Amplitude');
subplot(2,3,4);
v = prod(x1,x2);
stem(n,v,'o');
grid on;
title('x1[n].x2[n]');
xlabel('Time');
ylabel('Amplitude');
subplot(2,3,5);
x = x1;
for i = 1:max(n)
 x = [0,x];
 n = [-i,n];
end
x_r = flip(x);
w = conv(x,x_r);
w_n = n(1)+n(1):1:(length(w)+n(1)+n(1)-1);
stem(w_n,w);
grid on;
title('x1[n]*x1[-n]');
xlabel('Time');
ylabel('Amplitude');
subplot(2,3,6);
z = [];
z_n = [];
for i = 1:round(max(n)/2)
 z = [z,x1(2*i-1)];
 z_n = [z_n,i-1];
end
stem(z_n,z,'y');
grid on;
title('x1[2n]');
xlabel('Time');
ylabel('Amplitude');
function product = prod(x,y)
 product = x.*y;
end
OUTPUT: