声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1951|回复: 0

[共享资源] 心电滤波程序

[复制链接]
发表于 2006-10-25 21:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
请各位高手帮小弟对这个心电滤波程序作简单介绍,我看了半天看不懂,这个程序是用什么算法,用什么类型的滤波器?还有这个产生噪声心电波形和正常心电波形的程序写在哪一段.

  1. function sgolaydemo(action);
  2. %SGOLAYDEMO Demonstrates Savitzky-Golay filtering.
  3. %   This GUI demonstrates the smoothing obtained        
  4. %   with Savitzky-Golay filters on noisy ECG signals.  
  5. %                                                      
  6. %   Three plots are shown: The noisy ECG signal,
  7. %   the smoothed signal and the noiseless signal.              
  8. %                                                      
  9. %   The noise level can be changed with a slider                 
  10. %   control. The Savitzky-Golay parameters can be      
  11. %   varied; in particular the degree of the polynomial
  12. %   used to fit the data, and the frame size can be
  13. %   changed.

  14. %   Author(s): R. Losada
  15. %   Copyright 1988-2002 The MathWorks, Inc.
  16. %   $Revision: 1.11 $  $Date: 2002/03/28 12:47:39 $
  17. if nargin==0,
  18.    action = 'initialize';
  19. end

  20. x1 = ecg(500).';
  21. x = [x1;x1;x1;x1;x1;x1];
  22. y = sgolayfilt(x,0,15); %noiseless signal

  23. switch action,
  24. case 'initialize',
  25.    % Initialize Graphics
  26.    hfig = figure('NumberTitle','off',...
  27.       'Name','Savitzky-Golay Filtering Demo');
  28.   
  29.   bgc = get(0,'DefaultUIControlBackgroundColor');
  30.    % Frame for the all uicontrols
  31.    framepos = [.74 .05 .25 .85];
  32.    uicontrol( ...
  33.       'Style','frame',...
  34.       'Units','normalized',...
  35.       'BackgroundColor',bgc,...
  36.       'Position',framepos);   
  37.    % Frame for the parameters section
  38.    framepos2 = [.75 .36 .23 .23];
  39.    uicontrol( ...
  40.       'Style','frame',...
  41.       'Units','normalized',...
  42.       'ForegroundColor','black',...
  43.       'Position',framepos2);   
  44.   % Savitzky-Golay Filter Parameters Text
  45.   titlepos = [.76 .57 .17 .03];
  46.   titlelabel = ' Filter Parameters';
  47.   uicontrol( ...
  48.       'String',titlelabel,...
  49.       'HorizontalAlignment','left',...
  50.       'Style','text',...
  51.       'Units','normalized',...
  52.       'BackgroundColor',bgc,...
  53.       'ForegroundColor','black',...
  54.       'Position',titlepos);
  55.   % SG degree menu
  56.    menuname1 = 'Polynomial Degree';
  57.    tposition1 = [.77 .5 .2 .05];
  58.    uicontrol( ...
  59.       'String',menuname1,...
  60.       'HorizontalAlignment','left',...
  61.       'Style','text',...
  62.       'Units','normalized',...
  63.       'ForegroundColor','black',...
  64.       'Position',tposition1);
  65.    mposition1 = [.77 .46 .1 .05];
  66.    popstr1 = {'0','1','2','3','4'};
  67.    degreehndl = uicontrol('String',popstr1,...
  68.       'Style','Popup',...
  69.       'Tag','degreepopup',...
  70.       'Units','normalized',...
  71.       'Backgroundcolor','white',...
  72.       'Position',mposition1,...
  73.       'UserData',str2double(popstr1),...
  74.       'CallBack','sgolaydemo(''recal'')');
  75.    % SG frame size menu
  76.    menuname2 = 'Frame Size';
  77.    tposition2 = [.77 .4 .2 .05];
  78.    uicontrol( ...
  79.       'String',menuname2,...
  80.       'Style','text',...
  81.       'HorizontalAlignment','left',...
  82.       'Units','normalized',...
  83.       'ForegroundColor','black',...
  84.       'Position',tposition2);
  85.    mposition2 = [.77 .37 .1 .05];
  86.    popstr2 = {'5','15','25','55'};
  87.    framehndl = uicontrol('String',popstr2,...
  88.       'Style','Popup',...
  89.       'Tag','framepopup',...
  90.       'Backgroundcolor','white',...
  91.       'Units','normalized',...
  92.       'Position',mposition2,...
  93.       'UserData',str2double(popstr2),...
  94.       'CallBack','sgolaydemo(''recal'')');
  95.    % Noise variance slider
  96.    sliname1 = 'Noise Level';
  97.    tposition4 = [.76 .8 .21 .05];
  98.    uicontrol(...
  99.       'String',sliname1,...
  100.       'Style','text',...
  101.       'Units','normalized',...
  102.       'BackgroundColor',bgc,...
  103.       'ForegroundColor','black',...
  104.       'Position',tposition4);
  105.    mposition4 = [.76 .75 .21 .05];
  106.    sliderhndl = uicontrol('Style','slider',...
  107.       'Tag','noiseslider',...
  108.       'Units','normalized',...
  109.       'Value',0.7,...
  110.       'Min',0,'Max',1.4,...
  111.       'Position',mposition4,...
  112.       'CallBack','sgolaydemo(''recal'')');
  113.    slimin = 'Less';
  114.    tposition5 = [.76 .68 .07 .07];
  115.    uicontrol(...
  116.       'String',slimin,...
  117.       'Style','text',...
  118.       'Units','normalized',...
  119.       'HorizontalAlignment','left',...
  120.       'ForegroundColor','black',...
  121.       'BackgroundColor',bgc,...
  122.       'Position',tposition5);
  123.    slimax = 'More';
  124.    tposition6 = [.9 .68 .07 .07];
  125.    uicontrol(...
  126.       'String',slimax,...
  127.       'Style','text',...
  128.       'Units','normalized',...
  129.       'HorizontalAlignment','right',...
  130.       'ForegroundColor','black',...
  131.       'BackgroundColor',bgc,...
  132.       'Position',tposition6);
  133.    % Info and Close buttons
  134.    tposition7 = [.77 .18 .19 .1];
  135.    uicontrol(...
  136.       'String','Info',...
  137.       'Units','normalized',...
  138.       'Position',tposition7,...
  139.       'Callback','sgolaydemo(''info'')');
  140.    tposition8 = [.77 .06 .19 .1];
  141.    uicontrol(...
  142.       'String','Close',...
  143.       'Units','normalized',...
  144.       'ForegroundColor','black',...
  145.       'Position',tposition8,...
  146.       'Callback','sgolaydemo(''done'')');
  147.       
  148.    % Initial values
  149.    noise = 0.7;
  150.    degree = 0;
  151.    frame = 5;
  152.    subplot('Position',[.07 .05 .65 .23]), plot(y)
  153.    set(gca,'XTick',[])
  154.    axis([0 3000 -.6 .8])
  155.    title('Noiseless ECG')
  156.    ha1 = subplot('Position',[.07 .67 .65 .23]); plot(y)
  157.    set(gca,'XTick',[])
  158.    axis([0 3000 -.6 .8])
  159.    title('Noisy ECG')
  160.    ha2 = subplot('Position',[.07 .36 .65 .23]); plot(y)
  161.    set(gca,'XTick',[])
  162.    axis([0 3000 -.6 .8])
  163.    title('Filtered ECG')
  164.    ud.handles.ax = [ha1 ha2];
  165.    ud.handles.uis = [degreehndl framehndl sliderhndl];
  166.    set(hfig,'UserData',ud);
  167.    update_gui(y,noise,degree,frame)
  168.    
  169. case 'recal',
  170.    hfig = gcf;
  171.    ud = get(hfig,'UserData');
  172.    hn = ud.handles.uis(3);
  173.    hd = ud.handles.uis(1);
  174.    hf = ud.handles.uis(2);
  175.    
  176.    noise = get(hn,'Value');
  177.    degindx = get(hd,'Value');
  178.    deg = get(hd,'UserData');
  179.    degree = deg(degindx);
  180.    
  181.    frmindx = get(hf,'Value');
  182.    frm = get(hf,'UserData');
  183.    frame = frm(frmindx);
  184.    update_gui(y,noise,degree,frame)
  185.    
  186. case 'info',
  187.    ttlStr = 'Savitzky-Golay Filtering Demo';
  188.    
  189.    hlpStr1= ...                                             
  190.       ['This demo shows the smoothing of an electrocardiogram (ECG) signal' sprintf('\n')...
  191.          'by filtering the noisy ECG with a Savitzky-Golay FIR filter.' sprintf('\n')...
  192.          'You can vary the noise level of the ECG signal to be filtered with the' sprintf('\n')...
  193.          'slider control on the upper right of the GUI. The parameters of the' sprintf('\n')...
  194.          'Savitzky-Golay filter can be changed through the two popup menus provided.' sprintf('\n')...
  195.          '' sprintf('\n')...
  196.          'Savitzky-Golay filters act as smoothers by performing a least squares' sprintf('\n')...
  197.          'fitting of a frame of data to a polynomial of a given degree.' sprintf('\n')...
  198.          '' sprintf('\n')...
  199.          'The frame size indicates the number of samples you are using to perform'  sprintf('\n')...
  200.          'the smoothing for each data point. The degree is the order of the polynomial' sprintf('\n')...
  201.          'to which each frame of data is fitted.' sprintf('\n')...
  202.          '' sprintf('\n')...
  203.          'The noiseless or ideal ECG is shown at the bottom of the figure for' sprintf('\n')...
  204.          'comparison purposes'];
  205.             
  206.    helpwin(hlpStr1, ttlStr);
  207.    
  208. case 'done',
  209.    close(gcf);
  210. end        

  211. function [s,f] = update_signal(y,noise,degree,frame)
  212. %UPDATE_SIGNAL Recalculates the noisy ECG and the filtered ECG signals.
  213. v = 0.05*noise*randn(3000,1);    % noise
  214. s = y + v;         % noisy ecg
  215. f = sgolayfilt(s,degree,frame);

  216. function update_gui(y,noise,degree,frame)
  217. %UPDATE_GUI This function is called to update the GUI with the new values.
  218. hfig = gcf;
  219. [s,f] = update_signal(y,noise,degree,frame);
  220. ud = get(hfig,'UserData');
  221. axhdlnp = ud.handles.ax(1);
  222. axhdlfp = ud.handles.ax(2);
  223. hdn = get(axhdlnp,'Children');
  224. hdf = get(axhdlfp,'Children');
  225. set(hdn,'YData',s);
  226. set(hdf,'YData',f);

  227. function x = ecg(L)
  228. %ECG generator.
  229. %   x = ecg(L)
  230. %   generates piecewise linear ECG signal of length L
  231. %   must post-smooth it with an N-point smoother:
  232. %   y = sgolayfilt(x, d, F), usually with d=0, and N=3,5,9, etc.

  233. a0 = [0,1,40,1,0,-34,118,-99,0,2,21,2,0,0,0];               % template
  234. d0 = [0,27,59,91,131,141,163,185,195,275,307,339,357,390,440];
  235. a = a0 / max(a0);
  236. d = round(d0 * L / d0(15));            % scale them to fit in length L
  237. d(15)=L;

  238. for i=1:14,
  239.        m = d(i) : d(i+1) - 1;
  240.        slope = (a(i+1) - a(i)) / (d(i+1) - d(i));
  241.        x(m+1) = a(i) + slope * (m - d(i));
  242. end
复制代码

[ 本帖最后由 suffer 于 2006-10-26 17:59 编辑 ]

评分

1

查看全部评分

回复
分享到:

使用道具 举报

您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-9-25 03:18 , Processed in 0.089545 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表