0% found this document useful (0 votes)
22 views31 pages

SP Lab

Uploaded by

darshansrathod12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views31 pages

SP Lab

Uploaded by

darshansrathod12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Signal Processing Lab

Experiment-1
Signal Generation

Name: A R Santhosh Kumar


USN: 1MS22EC002

Discrete Time Signal


Date of Exp: 20/04/2024

1. Unit Impulse Signal


Code:
function[y,n]=santhu_impluseseq(a,b,c);
n=a:b;
y=(n-c)==0;
stem(n,y);
xlabel('n');
ylabel('y');
Output:
a) Plot δ(n) for -5 ≤ n ≤ 5

(b) Plot δ(n+3) for -4 ≤ n ≤ 6

1|Page
(c) Plot δ(n-2) for -5 ≤ n ≤ 7

2|Page
Assignment 1(a): Modify the program to generate impulse sequence of
amplitude ‘A’
Code:
function[y,n]=santhu_impluseseq(a,b,c,x);
n=a:b;
y=(n-c)==0;
stem(n,x*y);
xlabel('n');
ylabel('y');
Output:

Assignment 1(b): Implement the same using Python Code


Code:
#importing libraries
import numpy as np
import [Link] as plt
#Function to plot impulse sequence d(n)
def unit_impulse(a, n, A):
delta = []
for sample in n:
if sample == a:
3|Page
[Link](A)
else:
[Link](0)
return delta
a = 0 # Enter delay or advance
UL = 5
LL = -5
A=1
n = [Link](LL, UL+1, 1)
d = unit_impulse(a, n, A)
print(d)
[Link](n, d)
[Link]('n')
[Link]([Link](LL, UL+1, 1))
[Link]([Link](0, A+1, 1))
[Link]('$\delta$[n]')
[Link]('Unit Impulse $\delta$[n]')
Output:

4|Page
2. Unit Step Response
Code:
function[y,n]=santhu_stepseq(a,b,c);
n=a:b;
y=(n-c)>=0;
stem(n,y);
xlabel('n');
ylabel('y');
Output:
(a) Plot u(n) for –5 ≤ n ≤ 5

(b) Plot u(n+2) for -4 ≤ n ≤ 6

5|Page
(c) Plot u(n-1) for –5≤ n ≤7

Assignment 2(a): Modify the program to generate step sequence of amplitude


‘A’
Code:
function[y,n]=pc_stepseq(a,b,c,x);
n=a:b;
y=(n-c)>=0;
stem(n,x*y);
xlabel('n');
ylabel('y');
Output:

6|Page
Assignment 2(b): Implement the same using python code
Code:
import numpy as np
import [Link] as plt
def stepSequence(a,b,k):
if k<a or k>b:
raise ValueError("K value cannot be out of range ")
indices=[Link](a,b+1)
values = np.zeros_like(indices)
if k in indices:
values[indices >= k] = 1
return indices,values
def plot_step_sequence(indices, values):
[Link](figsize=(10, 4))
[Link](indices, values, basefmt=" ", linefmt='b- ',markerfmt='bo')
[Link]('Step Sequence')
[Link]('Index')
[Link]('Amplitude')
[Link](True)
[Link](indices[0], indices[-1])
[Link]()
k = -2
a = -7
b=4
indices, values = stepSequence(a,b,k)
plot_step_sequence(indices, values)

7|Page
Output:

3. Unit Ramp Sequence


Code:
function[y,n]=santhu_rampseq(a,b,c);
n=a:b;
y=((n-c)>=0).*(n-c);
stem(n,y);
xlabel('n');
ylabel('y');
Output:
(a) Plot r(n) for -5≤n ≤5

8|Page
(b) Plot r(n+2) for -4≤n ≤6

(c) Plot r(n-1) for -5≤n ≤7

9|Page
Assignment 3(a): Modify the program to generate negative ramp sequence of
amplitude ‘A’
Code:
function[y,n]=santhu_rampseq(a,b,c,x);
n=a:b;
y=((c-n)>=0).*(n>=c);stem(n,x*y);
xlabel('n');
ylabel('y');

Assignment 3(b): Implement the same using python code


Code:
import numpy as np
import [Link] as plt
def negrampseq(A,a,b,c):
if c>b or c<a:
raise ValueError("Value of C out of range")
indices=[Link](a,b+1)
values = np.zeros_like(indices)
for i in range(len(indices)):
if indices[i]>=c:
values[i]=(c-indices[i])*indices[i]
return indices,values
def negativerampplot(indices,values):
[Link](figsize=(10, 4))
[Link](indices, values, basefmt=" ", linefmt='b-', markerfmt='bo')
[Link]('Step Sequence')
[Link]('Index')
[Link]('Amplitude')
[Link](True)
[Link](indices[0], indices[-1])
[Link]()
indices,values=negrampseq(4,-7,2,-3)
negativerampplot(indices, values)

10 | P a g e
Output:

4. Real exponential sequence (e^an)


Code:
function [y,n] = expseq(a,b,c)
n=b:c;
y=exp(n*a);
stem(n,y)
title('real exp seq')
xlabel('index')
ylabel('amplitude')
Output:

(a) Plot e ^0.9n for -5≤ n ≤5

11 | P a g e
(b) Plot e ^-1.1n for -4 ≤ n ≤6

Assignment 4: Implement the same using python code.


Code:

12 | P a g e
import numpy as np
import math as mt
import [Link] as plt
def exponentialfunc(a,b,c):
if c<a or c>b:
raise ValueError("C should be within a and b")
indices = [Link](a,b+1)
values = np.zeros_like(indices)
for i in range(len(indices)):
if indices[i]>=c:
values[i] = [Link](c*indices[i])
return indices,values
def plot(indices,values):
[Link](figsize=(10, 4))
[Link](indices, values, basefmt=" ", linefmt='b-', markerfmt='bo')
[Link]('Real Exponential Sequence')
[Link]('Index')
[Link]('Amplitude')
[Link](True)
[Link](indices[0], indices[-1])
[Link]()
indices,values=exponentialfunc(-3,8,2)
plot(indices,values)
Output:

5. Complex exponential sequence (e^jwn)


Code:
n=-4:4;

13 | P a g e
y=exp(j*0.3*n);
subplot(2,1,1);stem(n,real(y));
subplot(2,1,2);
stem(n,imag(y));
figure
subplot(2,1,1);
stem(n,abs(y));
subplot(2,1,2);
stem(n,phase(y)*180/pi);
Output:

Assignment 5(a): Generate and plot (e (1+j0.3)n ) plot for -5≤n ≤5


Code:
n=-4:4;

14 | P a g e
y=exp((1+j*0.3*n);
subplot(2,1,1);
stem(n,real(y));
subplot(2,1,2);
stem(n,imag(y));
figure
subplot(2,1,1);stem(n,abs(y));
subplot(2,1,2);
stem(n,phase(y)*180/pi);
Output:

Assignment 5(b): Implement the same using python code.


Code:

15 | P a g e
import numpy as np
import [Link] as plt
def exponential_sequence(a, exponent, min, max):
n = [Link](min, max + 1)
sequence = a * [Link](exponent * n)
return sequence
a=1
exponent = 1 + 0.3jmin = 0
max = 20
sequence = exponential_sequence(a, exponent, min, max)
[Link](figsize=(12, 6))
[Link](2, 1, 1)
[Link]([Link](sequence), basefmt=" ",linefmt='b-', markerfmt='bo')
[Link]('Magnitude of the Exponential Sequence')
[Link]('n')
[Link]('Magnitude')
[Link](2, 1, 2)
[Link]([Link](sequence), basefmt=" ",linefmt='b-', markerfmt='bo')
[Link]('Phase of the Exponential Sequence')
[Link]('n')
[Link]('Phase')
plt.tight_layout()
[Link]()
Output:

16 | P a g e
6. Sinusoidal sequence
Code:
n=-10:10;
y=sin(2+0.1*pi*n);
stem(n,y);
A=1;
omega=2*pi/12;n=-10:10;
y=A*cos(omega*n);
stem(n,y);
xlabel('n');
ylabel('y');
Output:

Assignment 6(a): genererate same sequence dealy by 36 degree


Code:
n=-10:10;
y=sin(2*0.1*pi*n+(36*pi/180));
stem(n,y);
xlabel('n');
ylabel('y');

17 | P a g e
Output:

Assignment 6(b): Generate a cosine sequence of frequency f=0.2


cycles/sample.
Code:
A=1;omega=2*pi*0.2;
n=-10:10;
y=A*cos(omega*n);
stem(n,y);
xlabel('n');
ylabel('y');
Output:

18 | P a g e
Assignment 6(c): Implement the same using python code.
Code:
import numpy as np
import [Link] as plt
# Define parameters
frequency = 1 # Frequency of the sinusoid
amplitude = 1 # Amplitude of the sinusoid
phase_delay_deg = 36 # Phase delay in degrees
phase_delay_rad = [Link](phase_delay_deg) # Convert phase
delay to radians
num_samples = 100 # Number of samples
# Generate time values
t = [Link](0, 2*[Link], num_samples)
19 | P a g e
# Generate the sinusoidal sequence
sinusoid = amplitude * [Link](2 * [Link] * frequency * t + phase_delay_rad)
# Plot the sinusoidal sequence
[Link](t, sinusoid)
[Link]('Discrete Sinusoidal Sequence with Phase Delay')
[Link]('Time')
[Link]('Amplitude')
[Link](True)
[Link]()
Output:

Code:
import numpy as np
import [Link] as plt
# Parameters
N = 100 # Number of samples
f = 0.2 # Frequency of the cosine sequence
# Generate the discrete cosine sequence
n = [Link](N)
x = [Link](2 * [Link] * f * n)
# Plot the sequence
[Link](n, x, use_line_collection=True)
[Link]('Sample')
[Link]('Amplitude')

20 | P a g e
[Link]('Discrete Cosine Sequence (f=0.2 cycles/sample)')
[Link](True)
[Link]()
Output:

7. Exponential sequence (a^n )

Assignment 7(a): Generate the sequence x(n)=2 δ(n+2)- δ(n-4)


Code:
n = -10:10;
x = 2 * (n == -2) - (n == 4);
stem(n, x);
xlabel('Sample (n)');
ylabel('Amplitude');
title('Sequence x(n) = 2 * δ(n + 2) - δ(n - 4)');
Output:

21 | P a g e
Assignment 7(b): Generate the sequence X(n)=n[u(n)-u(n-10)]+10 e -0.3 (n-10)
[u(n-20)] , 0≤n ≤20
Code:
n = 0:20;
% Generate the sequence
X = n .* ( (n >= 0) - (n >= 10) ) + 10 * exp(-0.3 * (n - 10)) .* ( (n >= 10) - (n >= 20)
);
% Plot the sequence
stem(n, X);
xlabel('Sample (n)');
ylabel('Amplitude');
title('Sequence X(n)');
Output:

22 | P a g e
Assignment 7(c): Implement the same using python code.
Code:
import numpy as np
import [Link] as plt
# Define the range of samples
n = [Link](-10, 11)
# Generate the sequence
x = 2 * (n == -2) - (n == 4)
# Plot the sequence
[Link](n, x, use_line_collection=True)
[Link]('Sample (n)')
[Link]('Amplitude')
[Link]('Sequence x(n) = 2 δ(n+2) - δ(n-4)')
[Link](True)
23 | P a g e
[Link]()
Output:

Code:
import numpy as np
import [Link] as plt
# Define the range of samples
n = [Link](0, 21)
# Generate the sequence
X = n * ((n >= 0) & (n < 10)) + 10 * [Link](-0.3 * (n - 10)) *
((n >= 10) & (n < 20))
# Plot the sequence
[Link](n, X, use_line_collection=True)
[Link]('Sample (n)')
[Link]('Amplitude')
[Link]('Sequence X(n)')
[Link](True)

24 | P a g e
[Link]()
Output

 Continuous Time Signals


1. Step signal
Code:
function[y,t]=santhu_stepsig(a,b,c)
t=a:0.01:b;
y=(t-c)>=0;
plot(t,y)
xlabel('ínstance');
ylabel('amplitude');
Output:

25 | P a g e
2. Ramp signal
Code:
function[y,t]=santhu_rampsig(a,b,c)
t=a:0.1:b;
y=((t-c)>=0).*(t-c);
plot(t,y)
xlabel('ínstance');
ylabel('amplitude');
Output:

3.

26 | P a g e
[Link] exponential signal (e ^at)
Code:
function[y,t]=santhu_expsig(a,b,c)
t=b:0.01:c;
y=exp(a*t);
plot(t,y)
xlabel('ínstance');
ylabel('amplitude');
Output:

4. Complex exponential signal (e ^jwn)


Code:
t=-4:0.01:4;
y=exp(j*2*pi*50*t);
subplot(2,1,1);
plot(t,abs(y));
subplot(2,1,2);
plot(t,phase(y)*180/pi);
xlabel('time');
ylabel('X(T)');
Output

27 | P a g e
5. Sinusoidal signal
Code:
t=-10:0.01:10
y=sin(2*0.5*pi*t);
plot(t,y)
xlabel('Time(T)')
ylabel('Amplitude(A)')
Output:

28 | P a g e
Generate square and triangular waves
Code:
t=0:.001:1;
f1= input('Enter the frequency of square wave 1, fl = ')
sq_wave1= square(2*pi*f1*t);
plot(t, sq_wave1);
f2 = input('Enter the frequency of square wave 2, f2 = ')
sq_wave2=square(2*pi*f2*t,75);
figure;
plot(t, sq_wave2);
f3 = input('Enter the frequency of triangular wave, f3 =')
tri_wavel = sawtooth(2*pi*f3*t, 0.5);
figure;
plot(t, tri_wavel);
f4 = input('Enter the frequency of sawtooth wave 1, f4 = ')
saw_tooth1 = sawtooth(2*pi*f4*t,0);
figure;
plot(t, saw_tooth1);
f5 = input('Enter the frequency of sawtooth wave 2, f5 = ')

29 | P a g e
saw_tooth2 = sawtooth(2*pi*f5*t, 1);
figure;
plot(t,saw_tooth2);
Output:

Assignment: Implement the same using python code.


#importing libraries
import numpy as np
import [Link] as plt
import math
from scipy import signal
#Generate square wave of 50% duty cycle
t = [Link](0, 1, 1000, endpoint=True)
30 | P a g e
[Link]()
[Link](t, [Link](2 * [Link] * 10 *t))
[Link]('Time')
[Link]('Amplitude')
[Link]('Square wave (50% duty cyle)')
[Link](y = 0, color = 'k')
Output:

31 | P a g e

You might also like