A list,

ECG filtering based on MATLAB

Ii. Source code

function varargout = ECG(varargin)
% ECG MATLAB code for ECG.fig
%      ECG, by itself, creates a new ECG or raises the existing
%      singleton*.
%
%      H = ECG returns the handle to a new ECG or the handle to
%      the existing singleton*.
%
%      ECG('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ECG.M with the given input arguments.
%
%      ECG('Property'.'Value',...). creates anew ECG or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before ECG_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to ECG_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one % instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help ECG

% Last Modified by GUIDE v2. 5 22-May- 2020. 21:35:12

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @ECG_OpeningFcn, ...
                   'gui_OutputFcn',  @ECG_OutputFcn, ...
                   'gui_LayoutFcn', [],...'gui_Callback'[]);if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before ECG is made visible.
function ECG_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to ECG (see VARARGIN)

% Choose default command line output for ECG
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes ECG wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = ECG_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;



function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0.'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor'.'white');
end


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[FileName PathName]=uigetfile('*.mat'.'MAT-files(*.mat)'.'Select file');
str=[PathName FileName];
set(handles.edit1,'string',str);
Fs=200; N=512; MEAN=0; MIN=0; MAX=0; VAR=0; STD=0; RR=0; global im; global MEAN; global MIN; global MAX; global VAR; global STD; global RR;if strcmp(str,'D:\ ecg data \ Subject 2 ECG \chenwei1.mat')= =1
    im=1;
else
    im=2;
end
qq=get(handles.popupmenu1,'value')
bb=get(handles.popupmenu2,'value')
switch(im)
    case 1
            load('chenwei1.mat');
            ECG=xin(qq,:);
    case 2
           load('fanglipeng1.mat');
            ECG=xin(qq,:);
end

% uu1=get(handles.radiobutton7,'value');
% uu2=get(handles.radiobutton8,'value');
% uu3=get(handles.radiobutton8,'value');
% if uu1==1
%     MAX=max(EGC);
%     set(handles.edit3,'string',MAX);
% end
% if uu1==2
%     MIN=min(EGC);
%     set(handles.edit4,'string',MIN);
% end
% if uu1==3
%     MEAN=mean(EGC); 
%     set(handles.edit5,'string',MEAN);
% end
if          bb==1
            disp([num2str(bb)]);
            t=1/50:1/50:length(ECG)/50; MEAN=mean(xin(qq,:)); MAX=max(xin(qq,:)); MIN=min(xin(qq,:)); VAR=var(xin(qq,:),0.2); STD=std(xin(qq,:),0.2); axes(handles.axes3); grid on; plot(t,ECG); xlabel(Time (s)); ylabel('amplitude (V)');
            title('Original ECG signal time domain display');
            N=512;
            y=fft(ECG,N);
            mag=abs(y);
            f=(0:length(y)- 1)*Fs/length(y);
            axes(handles.axes5);
            plot(f,mag)
            xlim([0.100]);
            title('Spectrum of original ECG signal')
            xlabel('Frequency (Hz)'); ylabel('value'); axes(handles.axes8); histogram(ECG); xlabel('Signal amplitude'); ylabel('number');
            title('Distribution of ecg signals at different intervals');
elseif      bb==2disp([num2str(bb)]); % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- electrocardiosignal high-pass filter to baseline drift correction of -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- % % also can adopt method of elliptic filter or median to baseline drift correction (myMedfilt) and so on. fp=1.4; fr=0.05; rp=1; rs=30;
            wp=fp/(Fs/2); wr=fr/(Fs/2);
            [n,wn]=buttord(wp,wr,rp,rs);
            [b,a]=butter(n,wn,'high');
            [hw,w]=freqz(b,a);
            y2=filter(b,a,ECG);
            axes(handles.axes1);
            grid on;
            t1=0:1/50:(length(y2)- 1) /50;
            plot(t1,y2)
            title('Baseline drift removal time domain')
            xlabel(Time (s)); ylabel('amplitude (V)');
            y2=fft(y2,N);
            mfb=abs(y2);
            f=(0:length(y2)- 1)*Fs/length(y2);
            axes(handles.axes2);
            grid on;
            plot(f,mfb);
            xlim([0.100]);
            title('Amplitude and frequency of signal after baseline correction')
            xlabel('Frequency (Hz)'); ylabel('value');
elseif      bb==3
            disp([num2str(bb)]);
            fp=60; fs=100; % Passband cut-off frequency, stopband cut-off frequency rp=1.4; rs=1.6; % Passband and stopband attenuation WP =2*pi*fp; ws=2*pi*fs;     
            [n,wn]=buttord(wp,ws,rp,rs,'s'); % s' is to determine the butterworth analog filter order sum3DB cutoff analog frequency [z,P, K]= Buttap (n); % Normalized Butterworth simulation low-pass filter is designed, z is pole,P is zero and K is gain [BP, AP]= ZP2TF (z,P, K); % is converted to Ha(p),bp is the molecular coefficient,ap is the denominator coefficient [BS,as]= LP2LP (bp, AP, WP); %Ha(p) is converted to low pass Ha(s) and normalized, bs is the molecular coefficient, As is the denominator coefficient [hs, WS]= Freqs (BS, As); % amplitudefrequency response of analog filter [BZ, AZ]= Bilinear (BS, AS,Fs); % Bilinear transform for analog filter [H1, W1]=freqz(bz,az); The amplitude-frequency response of % digital filter m=filter(BZ, AZ,ECG); t1=0:1/50:(length(m)- 1) /50; axes(handles.axes1); grid on; axis tight; plot(t1,m) title('Time domain map of low-pass filtered ECG signal');
            xlabel(Time (s)); ylabel('amplitude (V)');
            y1=fft(m,N);
            mfa=abs(y1);
            f=(0:length(y1)- 1)*Fs/length(y1); axes(handles.axes2); grid on; axis tight; plot(f,mfa); xlim([0.100]);
            title('Spectrum of low-pass filtered ECG signals');
            xlabel('Frequency (Hz)'); ylabel('value');
elseif     bb==4 
           disp([num2str(bb)]);
           %50Hz notch filter: consists of a low-pass filter plus a high-pass filter % and the high-pass filter consists of an all-pass filter minus a low-pass filter Me=100; % Filter order L=100; % Window length beta=100; % attenuation coefficient Fs=200;  
            wc1=49/Fs*pi; %wc1 is the cut-off frequency of the high-pass filter, corresponding to51Hz  
            wc2=50/Fs*pi ; %wc2 is the cut-off frequency of the low-pass filter, corresponding to49Hz  
            h=ideal_lp(0.132*pi,Me)-ideal_lp(wc1,Me)+ideal_lp(wc2,Me); %h is the notch impact response w= Kaiser (L,beta); y=h.*rot90(w); % y is50Hz notch filter impact response sequence Y3 =filter(y,1,ECG); 
            t1=0:1/50:(length(y3)- 1) /50; axes(handles.axes1); grid on; axis tight; plot(t1,y3) title('Remove power frequency interference time domain time domain')
            xlabel(Time (s)); ylabel('amplitude (V)');
            y3=fft(y3,N);
            mfc=abs(y3);
            f=(0:length(y3)- 1)*Fs/length(y3); axes(handles.axes2); grid on; axis tight; plot(f,mfc); xlim([0.100]);
            title('Remove power frequency interference time domain Frequency domain');
            xlabel('Frequency (Hz)'); ylabel('value'); End %% % heart rate calculation % low pass filter FP =60; fs=100; % Passband cut-off frequency, stopband cut-off frequency rp=1.4; rs=1.6; % Passband and stopband attenuation WP =2*pi*fp; ws=2*pi*fs;     
[n,wn]=buttord(wp,ws,rp,rs,'s'); % s' is to determine the butterworth analog filter order sum3DB cutoff analog frequency [z,P, K]= Buttap (n); % Normalized Butterworth simulation low-pass filter is designed, z is pole,P is zero and K is gain [BP, AP]= ZP2TF (z,P, K); % is converted to Ha(p),bp is the molecular coefficient,ap is the denominator coefficient [BS,as]= LP2LP (bp, AP, WP); %Ha(p) is converted to low pass Ha(s) and normalized, bs is the molecular coefficient, As is the denominator coefficient [hs, WS]= Freqs (BS, As); % amplitudefrequency response of analog filter [BZ, AZ]= Bilinear (BS, AS,Fs); % Bilinear transform for analog filter [H1, W1]=freqz(bz,az); The amplitude-frequency response of % digital filter m=filter(BZ, AZ,ECG); fp=1.4; fr=0.05; rp=1; rs=30;
wp=fp/(fs/2); wr=fr/(fs/2);
[n,wn]=buttord(wp,wr,rp,rs);
[b,a]=butter(n,wn,'high'); [hw,w]=freqz(b,a); y2=filter(b,a,m); % high-pass filter fp=1.4; fr=0.05; rp=1; rs=30;
wp=fp/(Fs/2); wr=fr/(Fs/2);
[n,wn]=buttord(wp,wr,rp,rs);
[b,a]=butter(n,wn,'high'); [hw,w]=freqz(b,a); y2=filter(b,a,m); % band stop filter Me=100; % Filter order L=100; % Window length beta=100; % attenuation coefficient wc1=49/Fs*pi; %wc1 is the cut-off frequency of the high-pass filter, corresponding to51Hz  
wc2=50/Fs*pi ; %wc2 is the cut-off frequency of the low-pass filter, corresponding to49Hz  
h=ideal_lp(0.132*pi,Me)-ideal_lp(wc1,Me)+ideal_lp(wc2,Me); %h is the notch impact response w= Kaiser (L,beta); y=h.*rot90(w); % y is50Hz notch filter impact response sequence Y3 =filter(y,1,y2); % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- R wave detection and heart rate calculation -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- a =1; b=[0.000563925539225382.0.000687497480673608.0.000400105459896818.1.40107308696153 e-18.0.000167430218323421.4.18540736387490 e-19.0.000215512204999462.2.31378858508372 e-19.0.000825087119402599.0.00173867121147225.0.00170041274003132.2.89185229056478 e-18.0.00281247259984511.0.00484192899664537.0.00404159503274541.6.02125250728051 e-18.0.00516947529617596.0.00792307431385663.0.00588976752279114.7.41129393462523 e-18.0.00583817525454588.0.00764316928687033.0.00463943332803296.9.61260744539564 e-19.0.00196521978920439.2.40802770032636 e-18.0.00240551785915901.4.71696771800100 e-18.0.00853358817557591.0.0172851206658474.0.0162833499451329.1.24036605494920 e-17.0.0253107164699268.0.0426277900469897.0.0350794664744733.1.73330580157676 e-17.0.0449057473944898.0.0701489638765231.0.0540262508439817.3.98392673063995 e-17.0.0616816640708770.0.0915638898806427.0.0672043113722621.2.75356818775531 e-17.0.0700978687033445.0.0996538686301192.0.0700978687033445.2.75356818775531 e-17.0.0672043113722621.0.0915638898806427.0.0616816640708770.3.98392673063995 e-17.0.0540262508439817.0.0701489638765231.0.0449057473944898.1.73330580157676 e-17.0.0350794664744733.0.0426277900469897.0.0253107164699268.1.24036605494920 e-17.0.0162833499451329.0.0172851206658474.0.00853358817557591.4.71696771800100 e-18.0.00240551785915901.2.40802770032636 e-18.0.00196521978920439.9.61260744539564 e-19.0.00463943332803296.0.00764316928687033.0.00583817525454588.7.41129393462523 e-18.0.00588976752279114.0.00792307431385663.0.00516947529617596.6.02125250728051 e-18.0.00404159503274541.0.00484192899664537.0.00281247259984511.2.89185229056478 e-18.0.00170041274003132.0.00173867121147225.0.000825087119402599.2.31378858508372 e-19.0.000215512204999462.4.18540736387490 e-19.0.000167430218323421.1.40107308696153 e-18.0.000400105459896818.0.000687497480673608.0.000563925539225382];
d=filter(b,a,y3);
t=(0:(length(d)- 1))/Fs;
d1=sort(d,'descend'); d2=0;
for i=201 :2200
    d2=d1(i)+d2;
end;
d3=d2/2000;
[R_V,R_L]=findpeaks(d(12001:24000),'minpeakdistance'.1.'minpeakheight',d3);
d3=d2/2000;
[R_V,R_L]=findpeaks(d(12001:24000),'minpeakdistance'.1.'minpeakheight',d3); % According to the location and sampling frequency, calculate the average heart rate within the sampling interval H_Rate =60*(length(R_L)- 1)/((R_L(length(R_L))-R_L(1)) /200);
RR=num2str(H_Rate,3); % Calculate the sampling time interval %R_Time = R_L(length(R_L))/200; 


% --- Executes on button press in radiobutton7.

function radiobutton7_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global MAX;
 set(handles.edit3,'string',MAX);

% Hint: get(hObject,'Value') returns toggle state of radiobutton7


% --- Executes on button press in radiobutton8.
function radiobutton8_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global MIN;
set(handles.edit4,'string',MIN);
% Hint: get(hObject,'Value') returns toggle state of radiobutton8


% --- Executes on button press in radiobutton9.
function radiobutton9_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global MEAN;
 set(handles.edit5,'string',MEAN);
% Hint: get(hObject,'Value') returns toggle state of radiobutton9



function edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double


% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0.'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor'.'white');
end

Copy the code

3. Operation results

Fourth, note

Version: 2014 a