Undesired butterworth lowpass filtering graph in C++
10 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
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;
}
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Frequency Transformations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!