|
| 1 | +from src.signal_fitter import SignalFitter |
1 | 2 | from scipy import signal
|
2 | 3 | from typing import Optional
|
3 | 4 | import numpy as np
|
4 | 5 |
|
5 | 6 | class SignalFilter:
|
6 |
| - def __init__(self, noisySignal: np.ndarray, sampleRate: float = 1.0): |
7 |
| - self.sampleRate = sampleRate |
| 7 | + def __init__(self, timeVector: np.ndarray, noisySignal: np.ndarray): |
| 8 | + """ |
| 9 | + Initialize with a noisy signal to filter. |
| 10 | + Defaults: |
| 11 | + filterOrder: Order of the Butterworth filter (default is 4). |
| 12 | + cutoffFrequency: Cutoff frequency for the low-pass filter (default is 0.2). |
| 13 | + filterType: Type of filter ('butter', 'bessel', 'highpass'). Default is 'butter'. |
| 14 | + :param timeVector: The time vector associated with the signal. |
| 15 | + :param noisySignal: The noisy signal to be filtered. |
| 16 | + """ |
| 17 | + self.timeVector = timeVector |
8 | 18 | self.noisySignal = noisySignal
|
9 | 19 | self.filteredSignal: Optional[np.ndarray] = None
|
10 | 20 |
|
| 21 | + # Default parameters |
| 22 | + self.filterType: str = 'butter' |
| 23 | + self.filterOrder: int = 4 |
| 24 | + self.cutOffFrequency: float = 0.2 |
| 25 | + self.bType: str = 'low' |
| 26 | + |
11 | 27 | self.filterTypes = {
|
12 | 28 | "butter": lambda order, cutoff, btype: signal.butter(order, cutoff, btype, analog = False),
|
13 |
| - "chebyshev1": lambda order, cutoff, btype: signal.cheby1(order, 0.5, cutoff, btype, analog = False), |
14 |
| - "chebyshev2": lambda order, cutoff, btype: signal.cheby2(order, 20, cutoff, btype, analog = False), |
15 |
| - "elliptic": lambda order, cutoff , btype: signal.ellip(order, 0.5, 20, cutoff, analog = False), |
16 | 29 | "bessel": lambda order, cutoff, btype: signal.bessel(order, cutoff, btype, analog = False),
|
17 |
| - "notch": lambda notchFreq, Q: signal.iirnotch(notchFreq, Q, fs = self.sampleRate), |
18 |
| - "highpass": lambda order, cutoff, btype: signal.butter(order, cutoff, btype, analog = False), |
19 |
| - "bandpass": lambda low_cutoff, high_cutoff, btype: signal.butter(4, [low_cutoff, high_cutoff], btype, analog = False), |
20 |
| - "bandstop": lambda low_cutoff, high_cutoff, btype: signal.butter(4, [low_cutoff, high_cutoff], btype, analog = False) |
| 30 | + "highpass": lambda order, cutoff, btype: signal.butter(order, cutoff, btype, analog = False) |
21 | 31 | }
|
22 | 32 |
|
23 |
| - def apply_filter(self, filterType: str = 'butter', **kwargs) -> np.ndarray: |
| 33 | + self.applyFilter(order = self.filterOrder, cutoff = self.cutOffFrequency, btype = self.bType) |
| 34 | + |
| 35 | + def setFilterParameters(self, filterType: str, filterOrder: int, cutOffFrequency: float, bType: str) -> None: |
24 | 36 | """
|
25 |
| - Apply the specified filter type. |
26 |
| - :param filterType: Type of filter ('butter', 'chebyshev1', 'chebyshev2', 'elliptic', 'bessel', 'notch', 'highpass', 'bandpass', 'bandstop'). |
27 |
| - :param kwargs: Additional parameters for specific filters. |
28 |
| - :return: Filtered signal as a numpy array. |
| 37 | + Set or change filter parameters. |
| 38 | + :param filterType: Type of filter ('butter', 'bessel', 'highpass'). |
| 39 | + :param filterOrder: Order of the filter. |
| 40 | + :param cutOffFrequency: Cutoff frequency for the filter. |
| 41 | + :param bType: Filter band type ('lowpass', 'highpass', etc.). |
29 | 42 | """
|
30 |
| - if filterType not in self.filterTypes: |
31 |
| - raise ValueError(f"Filter type '{filterType}' is not recognized.") |
| 43 | + self.filterType = filterType |
| 44 | + self.throwIfNotSupported() |
| 45 | + self.filterOrder = filterOrder |
| 46 | + self.cutOffFrequency = cutOffFrequency |
| 47 | + self.bType = bType |
32 | 48 |
|
33 |
| - if self.noisySignal is None: |
34 |
| - raise ValueError("Noisy signal is not generated. Please call 'generateNoisySignal' first.") |
35 |
| - |
36 |
| - # Design Butterworth low-pass filter |
37 |
| - [filterCoefficientsB, filterCoefficientsA] = self.filterTypes[filterType](**kwargs) |
| 49 | + def throwIfNotSupported(self): |
| 50 | + """ |
| 51 | + Raise an error if the filter type is not supported. |
| 52 | + """ |
| 53 | + if self.filterType not in self.filterTypes: |
| 54 | + raise ValueError(f"Filter type '{self.filterType}' is not recognized.") |
38 | 55 |
|
| 56 | + def applyFilter(self, **kwargs) -> 'SignalFilter': |
| 57 | + """ |
| 58 | + Apply the configured filter to the noisy signal. |
| 59 | + :param kwargs: Additional parameters for specific filters. Given as Dict with keys: Order, cutoff, btype |
| 60 | + """ |
| 61 | + [filterCoefficientsB, filterCoefficientsA] = self.filterTypes[self.filterType](**kwargs) |
39 | 62 | self.filteredSignal = signal.filtfilt(filterCoefficientsB, filterCoefficientsA, self.noisySignal)
|
40 |
| - |
41 | 63 | print("Filter applied.")
|
| 64 | + return self |
42 | 65 |
|
43 |
| - return self.filteredSignal |
| 66 | + def fitDampedSineWave(self) -> SignalFitter: |
| 67 | + """ |
| 68 | + Create a SignalFitter instance to fit a damped sine wave to the filtered signal. |
| 69 | + :return: A SignalFitter instance. |
| 70 | + """ |
| 71 | + return SignalFitter(self.timeVector, self.filteredSignal) |
44 | 72 |
|
45 | 73 | def getFilteredSignal(self) -> Optional[np.ndarray]:
|
| 74 | + """ |
| 75 | + Retrieve the filtered signal. |
| 76 | + :return: The filtered signal or None if filtering hasn't been performed. |
| 77 | + """ |
46 | 78 | return self.filteredSignal
|
0 commit comments