Spaces:
Sleeping
Sleeping
File size: 1,721 Bytes
6a9b2e3 |
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
import numpy as np
class FIR:
"""
FIR (Finite Impulse Response) filter implementation
Converted from C implementation in FIRFilter.c
"""
def __init__(self, step_size):
"""Initialize the FIR filter"""
self.N = step_size
self.input_buffer = np.zeros(2 * step_size, dtype=float)
def check_range(input_val):
"""Limit the value to the range [-1.0, 1.0]"""
if input_val > 1.0:
return 1.0
elif input_val < -1.0:
return -1.0
else:
return input_val
def init_fir(step_size):
"""Create a new FIR filter instance"""
return FIR(step_size)
def process_fir_filter(fir, input_signal, filter_coefficients):
"""
Process input through the FIR filter
Args:
fir: FIR filter instance
input_signal: Input signal array
filter_coefficients: Filter coefficients array
Returns:
output_signal: Filtered output signal
"""
# Create output array
output = np.zeros(fir.N, dtype=float)
# Update input buffer
fir.input_buffer[: fir.N] = fir.input_buffer[fir.N :]
fir.input_buffer[fir.N :] = input_signal
# Apply FIR filter
for i in range(fir.N):
temp = 0.0
for j in range(len(filter_coefficients)):
idx = fir.N + (i - j)
if idx >= 0 and idx < 2 * fir.N: # Ensure index is in bounds
temp += fir.input_buffer[idx] * filter_coefficients[j]
output[i] = check_range(temp)
return output
def destroy_fir(fir):
"""
Clean up resources (not necessary in Python due to garbage collection)
Included for API compatibility with C code
"""
# In Python, memory management is automatic
pass
|