⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ire.m

📁 分辨率增强函数,一个独立的Matlab函数
💻 M
📖 第 1 页 / 共 2 页
字号:
function ire(x, signal)
% Interactive resolution enhancement for data in the arguments 
% "x,signal" written as a single self-contained matlab function.
% See http://www.wam.umd.edu/~toh/spectrum/InteractiveResEnhance.htm
% Displays sliders for separate real-time control of 2nd and 4th   
% derivative weighting factors (factor and factor2) and smooth 
% width. (Larger values of factor1 and factor2 will reduce the 
% peak width but will cause artifacts in the baseline near 
% the peak.  Adjust the factors for the the best compromise.)
% Use the minimum smooth width needed to reduce excess noise. 
% If the range of the sliders is inappropriate for your signal,
% you can adjust the slider ranges in lines 33-35.
% Declares global variables IREDATA and IREPARAMETERS.
% Filtered signal is returned in IREDATA(3,:,:),
% so declare IREDATA as global before calling ire.m to
% have access to IREDATA outside of ire.m
% Tom O'Haver (toh@umd.edu), June 2008. Slider function by 
% matthew jones (matt_jones@fastmail.fm), 2004

close
global IREDATA % IREDATA=[x;signal;enhancedsignal];
global IREPARAMETERS % IREPARAMETERS=[factor1 factor2 SmoothWidth]
global PlotRange
format compact
clf;hold off
enhancedsignal=ones(size(x));
IREDATA=[x;signal;enhancedsignal];

% Adjust x and y vector shape to 1 x n (rather than n x 1)
x=reshape(x,1,length(x));
signal=reshape(signal,1,length(signal));

% You can manually change the slider ranges in the next 3 lines
PeakWidth=length(signal)./5;
Factor1Range=2.*(PeakWidth.^2)/5;
Factor2Range=4.*(PeakWidth.^4)/370;
SmoothWidthRange=PeakWidth;
SmoothWidth=14;
factor1=0;
factor2=0;
% Plot the  signal
h=figure(1);
PlotRange=[SmoothWidth.*3:length(x)-SmoothWidth.*3];
plot(x(PlotRange),signal(PlotRange),'b')
h2=gca;
my=max(signal);
ny=min(signal);
axis([x(1) x(length(x)) ny my]);
% Label the plot
title(['factor1 = ' num2str(factor1)  '    factor2 = '  num2str(factor2) '    SmoothWidth= ' num2str(SmoothWidth)])
xlabel('BLUE = Original signal     RED = Resolution-enhanced signal')
grid on
IREPARAMETERS=[factor1 factor2 SmoothWidth];

% Add real-time sliders to graph for control of factor1 and factor2.
rtslid(h,@ResEnhanceF1,h2,1,'Scale',[0 Factor1Range],'Def',0,'Back',[0.9 0.9 0.9],'Label','Factor 1','Position',[0.03 0.5 0.03 0.35]);
rtslid(h,@ResEnhanceF2,h2,0,'Scale',[0 Factor2Range],'Def',0,'Back',[0.9 0.9 0.9],'Label','Factor 2','Position',[0.03 0.05 0.03 0.35]);
rtslid(h,@ResEnhanceF3,h2,0,'Scale',[0 SmoothWidthRange],'Def',SmoothWidth,'Back',[0.9 0.9 0.9],'Label','Smooth','Position',[0.95 0.1 0.03 0.35]);

function ResEnhanceF3(n,h)
% Redraw the graph when the SmoothWidth slider is changed
% Tom O'Haver, July 2006
global IREDATA % IREDATA=[x;signal;enhancedsignal];
global IREPARAMETERS % IREPARAMETERS=[factor1 factor2]
global PlotRange
x=IREDATA(1,:,:);
signal=IREDATA(2,:,:);
SmoothWidth=1+round(n);
IREPARAMETERS(3)=SmoothWidth;
factor1=IREPARAMETERS(1);
factor2=IREPARAMETERS(2);
PlotRange=[SmoothWidth.*3:length(x)-SmoothWidth.*3];
enhancedsignal=ResEnhanceRedraw(x,signal,factor1,factor2,SmoothWidth);
IREDATA(3,:,:)=enhancedsignal;
axes(h);
h2=gca;

function ResEnhanceF2(n,h)
% Redraw the graph when the factor2 slider is changed
% Tom O'Haver, July 2006
global IREDATA % IREDATA=[x;signal;enhancedsignal];
global IREPARAMETERS % IREPARAMETERS=[factor1 factor2 SmoothWidth]
global PlotRange
factor2=round(n);
IREPARAMETERS(2)=factor2;
factor1=IREPARAMETERS(1);
SmoothWidth=IREPARAMETERS(3);
x=IREDATA(1,:,:);
signal=IREDATA(2,:,:);
enhancedsignal=ResEnhanceRedraw(x,signal,factor1,factor2,SmoothWidth);
IREDATA(3,:,:)=enhancedsignal;
axes(h);
h2=gca;

function ResEnhanceF1(n,h)
% Redraw the graph when the factor1 slider is changed
% Tom O'Haver, July 2006
global IREDATA % IREDATA=[x;signal;enhancedsignal];
global IREPARAMETERS % IREPARAMETERS=[factor1 factor2 SmoothWidth]
global PlotRange
factor1=round(n);
IREPARAMETERS(1)=factor1;
factor2=IREPARAMETERS(2);
SmoothWidth=IREPARAMETERS(3);
x=IREDATA(1,:,:);
signal=IREDATA(2,:,:);
enhancedsignal=ResEnhanceRedraw(x,signal,factor1,factor2,SmoothWidth);
IREDATA(3,:,:)=enhancedsignal;
axes(h);
h2=gca;

function enhancedsignal=ResEnhanceRedraw(x,signal,factor1,factor2,SmoothWidth)
% Redraw the graph of DemoResEnhance when the any slider is changed
global IREDATA % IREDATA=[x;signal;enhancedsignal];
global IREPARAMETERS % IREPARAMETERS=[factor1 factor2 SmoothWidth PlotRange]
global PlotRange
x=IREDATA(1,:,:);
signal=IREDATA(2,:,:);
enhancedsignal=enhance(signal,factor1,factor2,SmoothWidth);
enhancedsignal(1:(SmoothWidth.*3))=signal(1:(SmoothWidth.*3));
enhancedsignal(length(x)-SmoothWidth.*3:length(x))=signal(length(x)-SmoothWidth.*3:length(x));
plot(x,signal, x(PlotRange),enhancedsignal(PlotRange),'r')
title(['factor1 = ' num2str(factor1)  '    factor2 = '  num2str(factor2) '    SmoothWidth= ' num2str(SmoothWidth)])
xlabel('BLUE = Original signal     RED = Resolution-enhanced signal')
my=max(signal);
ny=min(signal);
axis([x(1) x(length(x)) ny my]);
grid on

function Enhancedsignal=enhance(signal,factor1,factor2,SmoothWidth)
% Resolution enhancement function by derivative method. the
% arguments factor1 and factor 2 are 2nd and 4th derivative weighting
% factors. Larger values of factor1 and factor2 will reduce the 
% peak width but will cause artifacts in the baseline near 
% the peak.  Adjust the factors for the the best compromise. 
% Use minimum smooth width needed to reduce excess noise. 
% Use InteracticeResEnhance.m to adjust these factors 
% interactively on your own signals.
% Functions required: secderiv.m, tsmooth.m
d2=secderiv(signal);  % Computes second derivative
d4=secderiv(d2);   % Computes fourth derivative
Enhancedsignal = signal-factor1.*tsmooth(d2,SmoothWidth)+...
factor2.*tsmooth(tsmooth(tsmooth(d4,SmoothWidth),SmoothWidth),SmoothWidth);

function d=secderiv(a)
% Second derivative of vector using 3-point central difference.
%  T. C. O'Haver, 2006.
n=length(a);
for j = 2:n-1;
  d(j)=a(j+1) - 2.*a(j) + a(j-1);
end
d(1)=d(2);
d(n)=d(n-1);

function s=tsmooth(Y,w)
%  tsmooth(Y,w) smooths vector Y by a triangular function of halfwidth w
%  T. C. O'Haver, 1988.
v=conv(boxcar(w),boxcar(w));
S=conv(Y,v);
startpoint=(length(v) + 1)/2;
endpoint=length(Y)+startpoint-1;
s=S(startpoint:endpoint) ./ sum(v)

function h = rtslid(fig,f,hh,varargin)

%RTSLID Slider widget that responds to dragging realtime
%
%       h2 = RTSLID(h,@Fcn,h3) places a slider on the figure
%       with handle h, and returns the handle to the slider
%       in h2.  The second argument is a pointer to a
%       function that takes a two input arguments, which
%       should be the function you wish to be responding to
%       changes in the slider position.  The first of these
%       input arguments will be a number between 0 and 1
%       (if using the default scale) that corresponds to the
%       slider vertical position, while the second is the
%       handle of the axes to that responds to the slider
%       movement (as a result of Fnc). This handle should be
%       passed to RTSLID as the third input argument h3.
%
%       Alternatively instead of passing a function handle
%       as the second argument, a set of commands can be
%       passed as a string.  In order to use the slider
%       output value in such a set of commands, the lower
%       case variable 'out' should be used which contains
%       this number (Look at bottom of DEMO1.m for example).
%
%       h2 = RTSLID(h,@fcn,h3,style) allows the user to
%       specify the style of the slider by including an
%       optional third input argument.
%       The different styles affect the slider's response
%       to mouse movement, and are:
%
%               0 - (Default) Jumps to the mouse pointer
%                   on click, and locks to the mouse on drag.
%               1 - Only moves with mouse drags.
%
%       Additional parameters are passed as parameter pairs,
%       ie. rtslid(h,@fcn,h3,'param1',v1,'param2',v2,'...).
%       Inclusion of the fourth argument 'style' is optional.
%       These parameter names are listed below (all parameter
%       names are case insensitive) and all are optional:
%
%               'POSITION'  -   Control the position and size
%                               of the slider by passing a
%                               four element vector in the
%                               range [0 to 1] as a multiple
%                               of current figure size.
%                               Use [x y width height].
%                               {Default: [0.03 0.1 0.03 0.8]}
%               'BACK'      -   Either a string of the name
%                               of a valid colormap, or a 3-
%                               element RGB vector in the
%                               range [0 to 1].
%                               {Default: 'jet'}
%               'SCALE'     -   A two element vector of the
%                               lower and upper output limits
%                               for the slider.
%                               {Default: [0 1]}
%               'LABEL'     -   A string for the slider label.
%                               {Default: ''}
%               'DEF'       -   A default initial value (with
%                               respect to the SCALE), although
%                               does not cause output.
%                               {Default: 0.5}
%               'BUTMOT'    -   Additional mouse callback for
%                               WindowButtonMotionFcn of the
%                               slider figure, to allow other
%                               processes to use this callback.
%                               {Default: ''}
%               'MOUSEDOWN' -   Additional mouse callback for
%                               WindowButtonDownFcn of the
%                               slider figure, to allow other
%                               processes to use this callback.
%                               {Default: ''}
%               'MOUSEUP'   -   Additional mouse callback for
%                               WindowButtonDownFcn of the
%                               slider figure, to allow other
%                               processes to use this callback.
%                               {Default: ''}
%
%   matthew jones (matt_jones@fastmail.fm), 2004

DEFstyle = 0;
DEFback = 'jet';
DEFscale = [0 1];
DEFlabel = '';
DEFdef = 0.5;

style = DEFstyle;
back = DEFback;
scale = DEFscale;
label = DEFlabel;
def = DEFdef;

butmot = '';
mousedown = '';
mouseup = '';

if nargin>3,    % Set the style and any other parameters
    [style,args] = parseparams(varargin);
    if (length(style)==0),
        style = 0;
    else
        style = style{1};
    end
    try
        for l=1:(length(args)/2),
            inparam = args((l*2)-1);
            switch upper(inparam{1}),
            case 'POSITION' % Adjust the size and position of slider

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -