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!