0% found this document useful (0 votes)
18 views15 pages

Untitled Document

Uploaded by

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

Untitled Document

Uploaded by

Diptajeet Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Exp 2

import numpy as np
import matplotlib.pyplot as plt

# Given signal parameters


n = np.arange(-10, 11) # Discrete time values

# Generate the signal x[n]


x = np.zeros_like(n, dtype=float)
x[(n >= 0) & (n <= 4)] = 4 # Constant values for n = 0 to 4
x[(n >= -5) & (n <= -1)] = n[(n >= -5) & (n <= -1)] + 4 # Ramp for n = -5 to -1

# Define shifts
shift_left = 3
shift_right = -3 # Right shift is negative in discrete time

# Shift the signal


n_shifted_left = n + shift_left
n_shifted_right = n + shift_right

# Initialize shifted signals


x_shifted_left = np.zeros_like(n, dtype=float)
x_shifted_right = np.zeros_like(n, dtype=float)

# Assign values based on the shifted indices


for i in range(len(n)):
# Left shift (n - 3)
original_index_left = i - shift_left
if 0 <= original_index_left < len(n):
x_shifted_left[i] = x[original_index_left]

# Right shift (n + 3)
original_index_right = i - shift_right
if 0 <= original_index_right < len(n):
x_shifted_right[i] = x[original_index_right]

# Plot the signals


plt.figure(figsize=(10, 8))

# Original signal
plt.subplot(3, 1, 1)
plt.stem(n, x, 'r', markerfmt='ro', basefmt='r', linefmt='r')
plt.title('Original Signal x[n]')
plt.xlabel('n')
plt.ylabel('x[n]')
plt.grid()

# Left-shifted signal
plt.subplot(3, 1, 2)
plt.stem(n, x_shifted_left, 'b', markerfmt='bo', basefmt='b', linefmt='b')
plt.title('Signal x[n-3]')
plt.xlabel('n')
plt.ylabel('x[n-3]')
plt.grid()

# Right-shifted signal
plt.subplot(3, 1, 3)
plt.stem(n, x_shifted_right, 'g', markerfmt='go', basefmt='g', linefmt='g')
plt.title('Signal x[n+3]')
plt.xlabel('n')
plt.ylabel('x[n+3]')
plt.grid()

plt.suptitle('Graphical Representation of Signals')


plt.tight_layout()
plt.show()
Exp 1:
import numpy as np
import matplotlib.pyplot as plt

# Unit step signal


n = np.arange(-10, 11)
u = np.heaviside(n, 1)

# Unit impulse signal


d = np.where(n == 0, 1, 0)

# Exponential signal
a = 0.8
x_exp = a ** n

# Unit ramp signal


x_ramp = n * u

# Unit parabolic signal


x_parabolic = n ** 2 * u

# Unit rectangle pulse


x_rect = np.logical_and(n >= -5, n <= 5)

# Triangular signal
x_triangular = np.logical_and(n >= -5, n <= 5) * (1 - np.abs(n) / 5)

# Sinusoidal signal
f = 0.1
x_sine = np.sin(2 * np.pi * f * n)

# Symmetric (Even) and Antisymmetric (Odd) signals


x_even = (x_sine + np.flip(x_sine)) / 2
x_odd = (x_sine - np.flip(x_sine)) / 2
# Plotting
plt.figure(figsize=(12, 10))

plt.subplot(3, 3, 1); plt.stem(n, u); plt.title('Unit Step Signal')


plt.subplot(3, 3, 2); plt.stem(n, d); plt.title('Unit Impulse Signal')
plt.subplot(3, 3, 3); plt.stem(n, x_exp); plt.title('Exponential Signal')
plt.subplot(3, 3, 4); plt.stem(n, x_ramp); plt.title('Unit Ramp Signal')
plt.subplot(3, 3, 5); plt.stem(n, x_parabolic); plt.title('Unit Parabolic Signal')
plt.subplot(3, 3, 6); plt.stem(n, x_rect); plt.title('Unit Rectangle Pulse')
plt.subplot(3, 3, 7); plt.stem(n, x_triangular); plt.title('Triangular Signal')
plt.subplot(3, 3, 8); plt.stem(n, x_sine); plt.title('Sinusoidal Signal')
plt.subplot(3, 3, 9); plt.stem(n, x_even); plt.stem(n, x_odd, 'r');
plt.title('Symmetric and Antisymmetric Signals')
plt.legend(['Even', 'Odd'])

plt.tight_layout()
plt.show()
Ex 4

import numpy as np
import matplotlib.pyplot as plt

# Generate a sample input signal


n = np.arange(0, 11)
x = np.sin(0.1 * np.pi * n)

# Define the system coefficients (assuming a Moving Average


filter)
b = [1, 2, 1]
a = [1]

# Apply the system to the input using filter (more efficient)


y = np.convolve(b, x, mode='full')[0:len(n)] # Extract relevant part
for causality check

# Plot the input and output signals


plt.figure(figsize=(10, 6))
plt.subplot(2, 1, 1)
plt.stem(n, x, basefmt="b", label='Input (x[n])')
plt.title('Input Signal')
plt.xlabel('n')
plt.ylabel('x[n]')
plt.grid(True)

plt.subplot(2, 1, 2)
plt.stem(n, y, basefmt="r", label='Output (y[n])')
plt.title('Output Signal')
plt.xlabel('n')
plt.ylabel('y[n]')
plt.grid(True)

# Verify causality using output dependence on future input


samples
causality_msg = 'The system is causal.' if np.all(y == (x + 2 *
np.roll(x, 1)[:-1] + np.roll(x, 2)[:-2])) else 'The system is not
causal.'

# Add legend, adjust layout, and display causality message


plt.legend()
plt.tight_layout()
plt.figtext(0.15, 0.85, causality_msg, bbox=dict(facecolor='white',
alpha=1))
plt.suptitle('Causality Verification', y=1.02)

plt.show()
Ex 5

import numpy as np
import matplotlib.pyplot as plt

# Generate a sample input signal


n = np.arange(0, 11)
x1 = np.sin(0.1 * np.pi * n)
x2 = np.cos(0.2 * np.pi * n)

# Define the system coefficients


b = [2, -3, 1]
a = [1] # For simplicity, assume an FIR filter

# Apply the system to the input signals


y1 = np.convolve(x1, b, mode='same') # Output for x1
y2 = np.convolve(x2, b, mode='same') # Output for x2

# Verify linearity by applying superposition and scaling principles


alpha = 1.5
beta = -0.5
x_sum = alpha * x1 + beta * x2
y_sum = np.convolve(x_sum, b, mode='same') # Output for the
combined input

# Check if the system is linear


is_linear = np.allclose(y_sum, alpha * y1 + beta * y2)

# Plot input and output signals along with linear combinations


plt.figure(figsize=(10, 12))
# Input signals
plt.subplot(3, 1, 1)
plt.stem(n, x1, basefmt="b", linefmt="b-", markerfmt="bo",
label='x1[n]')
plt.stem(n, x2, basefmt="r", linefmt="r-", markerfmt="ro",
label='x2[n]')
plt.title('Input Signals')
plt.xlabel('n')
plt.ylabel('Amplitude')
plt.legend()
plt.grid(True)

# Output signals
plt.subplot(3, 1, 2)
plt.stem(n, y1, basefmt="b", linefmt="b-", markerfmt="bo",
label='y1[n]')
plt.stem(n, y2, basefmt="r", linefmt="r-", markerfmt="ro",
label='y2[n]')
plt.title('Output Signals')
plt.xlabel('n')
plt.ylabel('Amplitude')
plt.legend()
plt.grid(True)

# Linear combination verification


plt.subplot(3, 1, 3)
plt.stem(n, alpha * y1 + beta * y2, basefmt="g", linefmt="g-",
markerfmt="go", label='α · y1[n] + β · y2[n]')
plt.stem(n, y_sum, basefmt="k", linefmt="k-", markerfmt="ko",
label='T{α · x1[n] + β · x2[n]}')
plt.title('Linear Combinations')
plt.xlabel('n')
plt.ylabel('Amplitude')
plt.legend()
plt.grid(True)

# Add a message indicating linearity


msg = 'The system is linear.' if is_linear else 'The system is not
linear.'
plt.figtext(0.15, 0.02, msg, fontsize=12,
bbox=dict(facecolor='white', alpha=0.8))

plt.tight_layout()
plt.suptitle('Linearity Verification', y=1.02)
plt.show()
EX-6

import numpy as np
import matplotlib.pyplot as plt

# Define the system coefficients


b = [1, -1.5, 0.7] # Numerator coefficients
a = [1] # Denominator coefficients (FIR system)

# Generate a sample input signal


n = np.arange(0, 51)
x = np.sin(0.1 * np.pi * n)

# Apply the system to the input signal


y = np.convolve(x, b, mode='full') # Full convolution for visualization

# Compute the poles of the system


p = np.roots(a)

# Plot the poles on the complex plane


plt.figure(figsize=(10, 8))

plt.subplot(2, 1, 1)
plt.scatter(np.real(p), np.imag(p), marker='x', color='r', s=100, label='Poles')
plt.axhline(0, color='k', linewidth=0.5) # Real axis
plt.axvline(0, color='k', linewidth=0.5) # Imaginary axis
plt.grid(True)
plt.title('Pole-Zero Plot')
plt.xlabel('Real Part')
plt.ylabel('Imaginary Part')
plt.legend()

# Check stability and display a message


if all(np.abs(p) < 1): # Stability condition: poles inside the unit circle
msg = 'The system is stable.'
else:
msg = 'The system is unstable.'
plt.figtext(0.15, 0.45, msg, fontsize=12, bbox=dict(facecolor='white', alpha=1))

# Plot input and output signals


plt.subplot(2, 1, 2)
plt.stem(n, x, linefmt='b-', markerfmt='bo', basefmt='k', label='Input x[n]')
plt.stem(n, y[:len(n)], linefmt='r-', markerfmt='ro', basefmt='k', label='Output y[n]')
plt.title('Input and Output Signals')
plt.xlabel('n')
plt.ylabel('Amplitude')
plt.legend()
plt.grid(True)

plt.tight_layout()
plt.show()
Ex-7

import numpy as np
import matplotlib.pyplot as plt

def shift_signal(input_signal, shift_amount):


output_signal = np.roll(input_signal, shift_amount)
return output_signal

# Generate an input signal (e.g., a simple pulse)


Fs = 1000 # Sampling frequency
T = 1 / Fs # Sampling period
L = 100 # Length of signal
t = np.arange(0, L) * T # Time vector

# Create a pulse signal from sample 40 to 59


input_signal = np.zeros(L)
input_signal[40:60] = 1

# Shift the input signal by a specified amount


shift_amount = 10
output_signal = shift_signal(input_signal, shift_amount)

# Plot the input and output signals


plt.figure(figsize=(10, 6))

# Plot input signal


plt.subplot(2, 1, 1)
plt.stem(t, input_signal, basefmt="b-", markerfmt="bo", linefmt="b-")
plt.title("Input Signal")
plt.xlabel("Time (s)")
plt.ylabel("Amplitude")
plt.grid()
# Plot shifted output signal
plt.subplot(2, 1, 2)
plt.stem(t, output_signal, basefmt="g-", markerfmt="go", linefmt="g-")
plt.title("Output Signal (Shifted)")
plt.xlabel("Time (s)")
plt.ylabel("Amplitude")
plt.grid()

# Check if the system has shifting characteristics


# For a simple system like this, we assume the system does have shifting
characteristics
is_shifting = True

# Display a message about shifting characteristics


if is_shifting:
msg = 'The system has shifting characteristics.'
else:
msg = 'The system does not have shifting characteristics.'

# Display the message on the plot


plt.figtext(0.15, 0.1, msg, fontsize=12, bbox=dict(facecolor='white', alpha=1))

# Adjust layout and display the plot


plt.tight_layout()
plt.suptitle('Shifting Characteristics Verification', y=1.02)
plt.show()
Ex-8

import numpy as np
import matplotlib.pyplot as plt

# Define the parameters of the square wave


T = 2 # Period of the square wave
D = 0.7 # Duty cycle (portion of the period when the signal is
high)
N = 10 # Number of harmonics

# Time values
t = np.linspace(0, T, 1000)

# Initialize the Fourier series summation


f_fourier = np.zeros_like(t)

# Compute the Fourier series considering the duty cycle


for n in range(1, N+1):
k = 2*n - 1 # Harmonics are odd multiples (1, 3, 5, ...)
harmonic = (4 * D / (np.pi * k)) * np.sin(2 * np.pi * k * t / T)
f_fourier += harmonic

# Plot the square wave and its Fourier series approximation


plt.figure(figsize=(10, 6))
plt.plot(t, np.sign(np.sin(2 * np.pi * t / T)), 'b', linewidth=2) # Ideal
square wave
plt.plot(t, f_fourier, 'r--', linewidth=1) # Fourier series
approximation
plt.grid(True)
plt.title('Fourier Series of a Square Wave')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.legend(['Square Wave', 'Fourier Series Approximation'])
plt.show()

You might also like