Task 4-2: Apply Filters (30 points) You are required to implement the functions in…
- User
Task 4-2: Apply Filters (30 points) You are required to implement the functions in task_4_2.py. In this task, you are now given multiple noisy signals. Please use the filters that have been included in our tutorials to smooth these noisy data.
- task_4_2_1.pickle: You are supposed to filter it in apply_filter_1(). The sampling rate is 10Hz.
- task_4_2_2.pickle: You are supposed to filter it in apply_filter_2(). The sampling rate is 20Hz. Note: You can pick the filters according to the shape of the signals. Smoothing is a quite subjective task, but as a programming task, we have some quantitative metrics. The data for Task 4-2 are synthetic, meaning we possess the original datasets without any added noise. This allows for a direct comparison between the filtered output and the true, noise-free signal to objectively evaluate the effectiveness of the filtering process. Therefore, we can evaluate your results using the following metrics: • SNR (Signal-to-Noise Ratio): This measures the level of the desired signal relative to the back- ground noise. A higher SNR means the signal is clearer compared to the noise. • RMSE (Root Mean Square Error): RMSE quantifies the difference between values predicted by a model or filter and the values actually observed from the environment that is being modeled or about which predictions are being made. Lower RMSE values indicate a closer fit to the original data. COMP3516@HKU 1 Programming Task for Lab 4 Due: 2025-03-20 • Variations of the first derivative: This metric assesses the smoothness of a signal by measuring the changes in its rate of change. Smoother signals have less variation in their first derivative.
from scipy.signal import butter, sosfiltfilt from scipy.signal import savgol_filter from hampel import hampel import numpy as np import os.path as osp import pickle import matplotlib.pyplot as plt
class task_4_2: def init(self, data_root="./data/"): """ Initializes the task_4_1 class by loading signal data from a specified path.
Parameters: data_root (str): The root directory where the signal data file is located. The default value is "./data/".
Attributes: data (np.ndarray): Loaded signal data. fs (int): Sampling rate in Hz, initialized from the loaded data. """ file_1_n = "task_4_2_1.pickle" file_2_n = "task_4_2_2.pickle" with open(osp.join(data_root, file_1_n), "rb") as f: data1 = pickle.load(f) with open(osp.join(data_root, file_2_n), "rb") as f: data2 = pickle.load(f) self.data1, self.fs1 = data1["values"], data1["fs"] self.data2, self.fs2 = data2["values"], data2["fs"] self.clean1 = data1["clean"] self.clean2 = data2["clean"]
def apply_filter_1(self): """ Applies a specified filtering technique to smooth the noisy signal stored in self.data1.
This method is designed to process signals sampled at self.fs1, specifically for the data provided in 'task_4_2_1.pickle' (self.data1). The goal is to filter out noise and extract meaningful information from the signal using an appropriate digital filter.
- Assistant
- User
- Assistant
- User
still wrong maybe you can consider:
Task 4-1: apply_bandpass: the last doctest should be
round(filtered[10], 2), round(filtered[-1], 2) (0.44, 0.06)
Task 4-2: Remove the duplicated
Truefor apply_filter_2() - Assistant