MATLAB中采用Savitzky-Golay低通滤波器处理心跳录音信号的方法
Hey there! Let's break down exactly how to use MATLAB's Savitzky-Golay filter on your loaded heartbeat audio data, including parameter setup and proper function syntax.
First: Quick Recap of Your Setup
You already have your heartbeat audio loaded into a MATLAB variable (let's assume you used audioread, so your signal is stored in y with sampling rate Fs). If your audio is stereo, make sure to convert it to mono first (we'll include that in the example below).
Savitzky-Golay Filter Parameters: What to Choose?
The Savitzky-Golay filter relies on two key parameters—here's how to pick them for heartbeat signals:
- Polynomial Order: This controls how well the filter fits local signal trends. For heartbeat data (which has smooth, periodic patterns), stick to 2 or 3. Higher orders can overfit to noise, while lower orders might oversimplify the signal.
- Window Length: Must be an odd integer (since it centers on each data point). For typical audio sampling rates (e.g., 44100 Hz or 48000 Hz), start with values between 7 and 15.
- Larger window lengths = stronger smoothing (great for reducing high-frequency noise like microphone hiss)
- Smaller window lengths = preserves more signal detail (use if you notice the filtered signal is losing sharp heartbeat peaks)
How to Call the Filter (Where to Put Your WAV Variable)
MATLAB's sgolayfilt function is the tool you need, and your loaded WAV signal variable goes as the first input argument. Here's a complete example:
% 1. Load your WAV data (you've already done this, included for context) [y, Fs] = audioread('your_heartbeat_file.wav'); % Convert stereo to mono if needed if size(y, 2) > 1 y = y(:, 1); % Grab left channel; adjust to (:,2) for right if preferred end % 2. Set your Savitzky-Golay parameters poly_order = 2; % Start with 2, tweak if needed window_length = 11; % Odd integer, start with 11 % 3. Apply the filter: your WAV variable 'y' is the first argument y_filtered = sgolayfilt(y, poly_order, window_length);
Tuning Tips
- If the filtered signal still has too much high-frequency noise: Increase
window_length(try 13, 15) - If the heartbeat peaks look blurred/lost: Decrease
window_length(try 7,9) or lower the polynomial order to 1 - To visualize the difference between original and filtered signals, add this code:
t = (0:length(y)-1)/Fs; % Time vector for plotting figure; subplot(2,1,1); plot(t, y); title('Original Heartbeat Signal'); xlabel('Time (s)'); ylabel('Amplitude'); subplot(2,1,2); plot(t, y_filtered); title('Filtered Heartbeat (Savitzky-Golay)'); xlabel('Time (s)'); ylabel('Amplitude');
内容的提问来源于stack exchange,提问作者Mayur Makhija

