-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoef.py
More file actions
51 lines (41 loc) · 1.7 KB
/
Copy pathcoef.py
File metadata and controls
51 lines (41 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import firwin, freqz
# Define parameters
fs = 20000 # Sampling frequency (48 kHz)
nyquist = fs / 2 # Nyquist frequency
# Bandpass filter specifications
lowcut = 820 # Low cutoff frequency (15 kHz)
highcut = 960 # High cutoff frequency (17 kHz)
numtaps = 48 # Number of filter coefficients
# Design the FIR filter using the window method
coefficients = firwin(numtaps, [lowcut, highcut], pass_zero=False, fs=fs)
# Normalize coefficients to fixed-point representation (Q15 format)
fixed_point_coefficients = np.round(coefficients * 32767).astype(np.int16)
# Save the fixed-point coefficients to a text file
with open('F:/BTL3 AUDIO EQUALIZER/cof/filter500.txt', 'w') as f:
for coeff in fixed_point_coefficients:
f.write(f"{coeff}\n")
# Compute the frequency response of the filter
w, h = freqz(coefficients, worN=1024)
# Convert frequency response to fixed-point
fixed_point_response = np.round(np.abs(h) * 32767).astype(np.int16)
# Plot the frequency response
plt.figure(figsize=(12, 6))
plt.plot(w / np.pi * nyquist, 20 * np.log10(np.abs(h)), 'b')
plt.title('Frequency Response of the Bandpass FIR Filter')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Gain (dB)')
plt.xlim(0, nyquist)
plt.ylim(-60, 5)
plt.grid()
plt.axvline(lowcut, color='green', linestyle='--', label='Low Cutoff ')
plt.axvline(highcut, color='red', linestyle='--', label='High Cutoff')
plt.legend()
plt.show()
# Print the fixed-point filter coefficients
print("Fixed-Point Filter Coefficients (Q15 format):")
print(fixed_point_coefficients)
# Print the fixed-point frequency response
print("Fixed-Point Frequency Response (Magnitude in Q15 format):")
print(fixed_point_response)