24/11/2024, 21:49 RaisedCosineFilter
In [ ]:
Importing numpy and matplotlib.plt libraries
In [1]: import numpy as np
import matplotlib.pyplot as plt
Defining the function of a raised cosine
In [2]: def raised_cosine(f, T, r):
"""
Raised cosine filter in frequency domain.
Parameters:
f: Frequency array (Hz)
T: Symbol period (s)
r: Roll-off factor (0 <= r <= 1)
Returns:
H: Filter response (magnitude) in frequency domain
"""
B = (1 + r) / (2 * T) # Bandwidth
H = np.zeros_like(f)
for i, freq in enumerate(f):
abs_f = abs(freq)
if abs_f <= (1 - r) / (2 * T):
H[i] = 1
elif (1 - r) / (2 * T) < abs_f <= (1 + r) / (2 * T):
H[i] = 0.5 * (1 + np.cos(np.pi * T / r * (abs_f - (1 - r) / (2 * T))
else:
H[i] = 0
return H
Intiating the parameters of the raised cosine function
In [3]: # Parameters
T = 1 # Symbol period (s)
fs = 10 * T # Sampling frequency (Hz)
f = np.linspace(-2 / T, 2 / T, 1000) # Frequency range
# Roll-off factors
roll_off_factors = [0, 0.2, 0.5, 1]
Raised cosine spectrum in frequency domain
In [4]: # Frequency domain plots
plt.figure(figsize=(12, 6))
for r in roll_off_factors:
H = raised_cosine(f, T, r)
plt.plot(f, H, label=f"r = {r}")
plt.title("Raised Cosine Spectrum (Frequency Domain)")
file:///C:/Users/bonfa/Documents/Machine Learning/RaisedCosineFilter.html 1/3
24/11/2024, 21:49 RaisedCosineFilter
plt.xlabel("Frequency (Hz)")
plt.ylabel("Magnitude")
plt.grid()
plt.legend()
plt.show()
Raised cosine impulse response in time domain
In [5]: # Time domain impulse responses
time = np.linspace(-5 * T, 5 * T, 1000) # Time range
plt.figure(figsize=(12, 6))
for r in roll_off_factors:
H_f = raised_cosine(f, T, r)
h_t = np.fft.ifftshift(np.fft.ifft(np.fft.fftshift(H_f))) # IFFT to get imp
plt.plot(time, np.abs(h_t), label=f"r = {r}")
plt.title("Raised Cosine Impulse Response (Time Domain)")
plt.xlabel("Time (s)")
plt.ylabel("Amplitude")
plt.grid()
plt.legend()
plt.show()
file:///C:/Users/bonfa/Documents/Machine Learning/RaisedCosineFilter.html 2/3
24/11/2024, 21:49 RaisedCosineFilter
file:///C:/Users/bonfa/Documents/Machine Learning/RaisedCosineFilter.html 3/3