Undesired butterworth lowpass filtering graph in C++

10 ビュー (過去 30 日間)
Mohammed Adhil S
Mohammed Adhil S 2023 年 1 月 31 日
回答済み: Sivapriya Srinivasan 2023 年 2 月 28 日
Hi all,
I am trying to remove a high frequency noise in ECG data using butterworth lowpass filter at normalized cut-off frequency of 0.05 Hz and sampling rate of 500. Using Matlab I can able to remove the noise successfully by following the above mentioned specifications. When I use same coefficients in my c++ code, I am not getting the desired output instead I am getting a random graph. I have been working it for the long to sort out but still I am clueless. Can anyone help me in sorting it out. Thanks in advance.

回答 (1 件)

Sivapriya Srinivasan
Sivapriya Srinivasan 2023 年 2 月 28 日
I understand that you are able to remove high frequency noise in ECG data using MATLAB, but unable to implement the same using C++.
You can implement the Butterworth lowpass filter using digital signal processing library. Here is a sample code without ECG data array input:
#include <iostream>
#include <vector>
#include <dsp.h>
#include <cmath>
using namespace std;
int main(){
// number of samples in ECG data
const int N = 1000;
// normalized cutoff frequency
const double f0 = 0.05;
// sampling rate (Hz)
const double fs = 500.0;
// cutoff frequency (Hz)
const double fc = f0 * fs / 2.0;
// Your ECG data array input
double ecg_data[N] = { ... };
// initialize Butterworth lowpass filter
// filter coefficients
vector<double> b, a;
// order of filter
const int order = 4;
// calculate filter coefficients
butter(order, fc, fs, LOWPASS, b, a);
// filter ECG data
double ecg_filtered[N];
for (int i = 0; i < N; i++) {
ecg_filtered[i] = 0;
// apply filter
for (int j = 0; j <= order; j++) {
if (i - j >= 0) {
ecg_filtered[i] += b[j] * ecg_data[i - j];
}
if (i - j - 1 >= 0) {
ecg_filtered[i] -= a[j + 1] * ecg_filtered[i - j - 1];
}}}
// output filtered ECG data
for (int i = 0; i < N; i++) {
cout << ecg_filtered[i] << " ";
}
cout << endl;
return 0;
}

カテゴリ

Help Center および File ExchangeFrequency Transformations についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by