Code covered by the BSD License  

Highlights from
CubicSplineSmoothinV2

image thumbnail

CubicSplineSmoothinV2

by

 

01 Nov 2011 (Updated )

A GUI for smoothing noisy data.

CubicSplineSmoothinV2(varargin)
%% A Tool for smoothing noisy data
% Written by L M Vhengani
% 2011 October
% Requires Spline Toolbox
% This GUI read an array variable named 'y' and plots it on the axis
% The user can then slide the slider to smooth the displayed y data. an in
% this process the smooth data is also smoothed to compare with noisy. 
% The user can save the smoothed data by pressing the 'Save' button.
% The user can also change the range of the slider by inserting values and
% press the set button. Smoothed data is saved in the base workspace with the default name 'smoothy'. 
% The user can also change the name of the saved data to any name allowable
% within MATLAB.

%%EXAMPLE 1
% N = 100;
% x = linspace(-pi, pi, N);
% y = sin(x);
% % add 10% noise based on gaussian
% scale = 0.1;
% n1 = randn(1, N); % noise with mean=0 and std=1;
% y = y + n1.*y*scale;
% CubicSplineSmoothinV2

%%EXAMPLE 2
% N = 100;
% x = linspace(-pi, pi, N);
% y = exp(-x);
% % add 10% noise based on gaussian
% scale = 0.1;
% n1 = randn(1, N); % noise with mean=0 and std=1;
% y = y + n1.*y*scale;
% CubicSplineSmoothinV2

%%EXAMPLE 3
% N = 100;
% x = linspace(-pi, pi, N);
% y = sin(x);
% % add 10% noise based on gaussian
% scale = 0.1;
% n1 = randn(1, N); % noise with mean=0 and std=1;
% y = y + n1.*y*scale;
% CubicSplineSmoothinV2

%% THE MAIN GUI PROGRAM STARTS HERE
function varargout = CubicSplineSmoothinV2(varargin)
%CUBICSPLINESMOOTHINV2 M-file for CubicSplineSmoothinV2.fig
%      CUBICSPLINESMOOTHINV2, by itself, creates a new CUBICSPLINESMOOTHINV2 or raises the existing
%      singleton*.
%
%      H = CUBICSPLINESMOOTHINV2 returns the handle to a new CUBICSPLINESMOOTHINV2 or the handle to
%      the existing singleton*.
%
%      CUBICSPLINESMOOTHINV2('Property','Value',...) creates a new CUBICSPLINESMOOTHINV2 using the
%      given property value pairs. Unrecognized properties are passed via
%      varargin to CubicSplineSmoothinV2_OpeningFcn.  This calling syntax produces a
%      warning when there is an existing singleton*.
%
%      CUBICSPLINESMOOTHINV2('CALLBACK') and CUBICSPLINESMOOTHINV2('CALLBACK',hObject,...) call the
%      local function named CALLBACK in CUBICSPLINESMOOTHINV2.M with the given input
%      arguments.
%
%      *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 CubicSplineSmoothinV2

% Last Modified by GUIDE v2.5 21-Oct-2011 15:20:59

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @CubicSplineSmoothinV2_OpeningFcn, ...
                   'gui_OutputFcn',  @CubicSplineSmoothinV2_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 CubicSplineSmoothinV2 is made visible.
function CubicSplineSmoothinV2_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   unrecognized PropertyName/PropertyValue pairs from the
%            command line (see VARARGIN)
cla(handles.PlotArea,'reset')  % refreshing the plot area
global smoothflag; % once the data is smoothed this flag in truned on (i.e smoothflag=1)
global x      % make x global
global y    % make ESF global
smoothflag =0;
set(handles.Outname,'String','smoothy'); % set the name of the output variable to 'smoothy'
set(handles.Pvalueslide,'Value',1); % set P as new min value of P 1
set(handles.editPvalue,'String','1') % On the Edit Text show 1 as the P Value 
y = double(evalin('base', 'y')); % Get the noisy data from the base work space
n = length(y); x = 1:1:n;           % compute the array values of x
plot(x,y,'r'),xlabel('x'),ylabel('y'),legend('Data','Location','Best'),grid on % Plot the noisy signal


  minPv = get(handles.Pvalueslide,'Min'); % Get minimum value of the slider
          
        set(handles.MinP,'String',num2str(minPv))  % Set the minimum range on the minimum range edit test box 
        set(handles.MinStaticText,'String',num2str(minPv))  % Set the minimum range on the minimum range static text box 
        
  maxPv = get(handles.Pvalueslide,'Max');  % Get maximum value of the slider

        set(handles.MaxP,'String',num2str(maxPv))  % Set the maximum range on the maximum range edit test box  
        set(handles.MaxStaticText,'String',num2str(maxPv))  % Set the maximum range on the maximum range static text box 
        
        if smoothflag ==0;
           set(handles.SaveButton,'String','No Saving') 
        end
% Choose default command line output for CubicSplineSmoothinV2
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);
set(hObject,'toolbar','figure');
% UIWAIT makes CubicSplineSmoothinV2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = CubicSplineSmoothinV2_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 MinP_Callback(hObject, eventdata, handles)
% hObject    handle to MinP (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 MinP as text
%        str2double(get(hObject,'String')) returns contents of MinP as a double


% --- Executes during object creation, after setting all properties.
function MinP_CreateFcn(hObject, eventdata, handles)
% hObject    handle to MinP (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



function MaxP_Callback(hObject, eventdata, handles)
% hObject    handle to MaxP (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 MaxP as text
%        str2double(get(hObject,'String')) returns contents of MaxP as a double


% --- Executes during object creation, after setting all properties.
function MaxP_CreateFcn(hObject, eventdata, handles)
% hObject    handle to MaxP (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 SetPbutton.
function SetPbutton_Callback(hObject, eventdata, handles)
% hObject    handle to SetPbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global smoothflag;
smoothflag=0; % the data has been smoothed
        if smoothflag ==0
           set(handles.SaveButton,'String','No Saving') 
        end
        
        
axes(handles.PlotArea)
  currentP = get(handles.Pvalueslide,'Value'); % Getting the Current P value for the Slider 
  global x
  global y
  minPvn = str2double(get(handles.MinP,'String')); % Getting the new minimum P value from the MinP edit Text
  maxPvn = str2double(get(handles.MaxP,'String')); % Getting the new maximun P value from the MaxP edit Text
  
  % making sure that the minimum is not zero when using algorithm mode
  typec = get(handles.NumType,'Value');  % Get the type of numericals 
    if typec==2 % typec=2 is a logarthm mode
        if (minPvn==0)
            minPvn=1; % set the min to 1 if in logarithmic mode
            maxPvn=1000; % set the min to 1 if in logarithmic mode
            error('Log of 0 is infinity, always use a value greater than 0 for the minimum')
        end
    end % end the check and settings
 typec = get(handles.NumType,'Value');  % get the type of values to use, normal numeric or logarithmic 
% if new minPvn is less or if maxPvn is greater than the current P value
 if  (minPvn > currentP)||(maxPvn < currentP) || (maxPvn < minPvn)  % if statement for handling new min and max value for the slider
     
             if (maxPvn < minPvn) % making sure that the order is always right (maxPvn is always greater than minPvn)
                   a = maxPvn;
                   b = minPvn;
                   minPvn =a;  % exchanging
                   maxPvn = b; % exchanging
             end
        
        if (minPvn > currentP)
           set(handles.Pvalueslide,'Value',minPvn); % set P as new min value of P minPvn
           if typec ==2
               p = 10^(-log10(minPvn));
           elseif typec==1
               p = minPvn;
           end
           smoothy = csaps(x,y,p); % Recompute the smoothed ESF using the minimum p Value 
           cla(handles.PlotArea,'reset')    % Reflesh the plotting area
           plot(x,y,'r'),hold on,fnplt(smoothy),xlabel('x'),ylabel('y'),legend('Data','Smooth Data','Location','Best'),grid on % Plot the noisy signal and the smoothed signal
           set(handles.editPvalue,'String', num2str(p)); % display the current P value

        elseif (maxPvn < currentP)
           set(handles.Pvalueslide,'Value',maxPvn); % set P as new max value of P maxPvn
           if typec ==2
               p = 10^(-log10(maxPvn));
           elseif typec==1
               p = maxPvn;
           end
           smoothy = csaps(x,y,p);  % Recompute the smoothed ESF using the maximum p Value 
           cla(handles.PlotArea,'reset')   % Reflesh the plotting area
           plot(x,y,'r'),hold on,fnplt(smoothy),xlabel('x'),ylabel('y'),legend('Data','Smooth Data','Location','Best'),grid on % Plot the noisy signal and the smoothed signal
           set(handles.editPvalue,'String', num2str(p)); % display the current P value
        end
             
 end
% set the new  minimum and maximum vallue of the slider
        set(handles.Pvalueslide,'Min',minPvn) % set the new minimum value of the slider. This value is from the edit box
        set(handles.Pvalueslide,'Max',maxPvn) % set the new maximum value of the slider. This value is from the edit box
        set(handles.MinP,'String',num2str(minPvn)) % Show the current minimum value minimum value text edit box 
        set(handles.MaxP,'String',num2str(maxPvn)) % Show the current maximum value minimum value text edit box 
        if minPvn ==0   % Handling the format for values less than 0.001. If less than 0.001 write in e-04
                mintxt = num2str(minPvn); 
        elseif minPvn < 0.001
                mintxt = num2str(minPvn,'%6.1e\n'); % This is done to limi the number of zeros dispalyed on the text box
        else
                mintxt = num2str(minPvn); 
        end
        set(handles.MinStaticText,'String',mintxt)  % Set the minimum range on the minimum range static text box 
        if maxPvn <=1000   % Handling the format for values greater than 1000. If greater than 1000 write in e04
                maxtxt = num2str(maxPvn);
        elseif maxPvn > 1000
                maxtxt = num2str(maxPvn,'%6.1e\n'); % This is done to limit the number of zeros dispalyed on the text box
        end
        set(handles.MaxStaticText,'String',maxtxt)  % Set the maximum range on the maximum range static text box 
        
        guidata(hObject, handles); %updates the handles


% --- Executes on selection change in NumType.
function NumType_Callback(hObject, eventdata, handles)
% hObject    handle to NumType (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns NumType contents as cell array
%        contents{get(hObject,'Value')} returns selected item from NumType
global smoothy %
global y %
global x %
global smoothflag;
smoothflag=0; % the data has been smoothed
        if smoothflag ==0
           set(handles.SaveButton,'String','No Saving') 
        end
typec = get(handles.NumType,'Value');  % Get the type of numericals 

if typec==1
    p2 =1;
        set(handles.Pvalueslide,'Value',1); % set P as new min value of P 1
        set(handles.Pvalueslide,'Min',0)   % set the slider minimum to 0
        set(handles.Pvalueslide,'Max',1)   % set the slider maximum to 1
        set(handles.MinP,'String',num2str(0))  % set the slider minimum to 0
        set(handles.MaxP,'String',num2str(1))  % set the slider maximum to 1
        set(handles.editPvalue,'String', num2str(1)); % set the slider value at 1
        set(handles.MinStaticText,'String',num2str(0))
        set(handles.MaxStaticText,'String',num2str(1))
elseif typec==2
        p2=10^(-log10(1));
        set(handles.Pvalueslide,'Value',p2); % set P as new min value of P 1
        set(handles.Pvalueslide,'Min',1)     % set the slider minimum to 1
        set(handles.Pvalueslide,'Max',1000)  % set the slider maximum to 1000
        set(handles.MinP,'String',num2str(1)) % display on the edit text box the minimum p range as 1
        set(handles.MaxP,'String',num2str(1000)) % display on the edit text box the maximum p range as 1000
        set(handles.editPvalue,'String', num2str(p2)); % set the slider value at 1
        set(handles.MinStaticText,'String',num2str(1))
        set(handles.MaxStaticText,'String',num2str(1000))
end

           smoothy = csaps(x,y,p2);   % Reset the smoothed ESf to the unsmoothed data   
           cla(handles.PlotArea,'reset')  % refresh the plot area
           plot(x,y,'r'),hold on,fnplt(smoothy),xlabel('x'),ylabel('y'),legend('Data','Smooth Data','Location','Best'),grid on % Plot the noisy signal and the smoothed signal

 guidata(hObject, handles); %updates the handles


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

% Hint: popupmenu 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 slider movement.
function Pvalueslide_Callback(hObject, eventdata, handles)
% hObject    handle to Pvalueslide (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
axes(handles.PlotArea)
global y       % The value ESF is already avalable for use, just declaring it so that it can be available for the function
global smoothy % declaring a global value smmothESF
global x          % x is already available: declaring it for usage
global smoothflag; % once the data is smoothed this flag in truned on (i.e smoothflag=1)
typec = get(handles.NumType,'Value');  % get the type of values to use, normal numeric or logarithmic


if typec==1   % if normal numeric
        p2 = get(handles.Pvalueslide,'Value');  % Getting the value of the slider
        
        set(handles.editPvalue,'String', num2str(p2)); % Show the current value on the P value text edit box 
              
        [smoothy p] = csaps(x,y); % Smoothing the ESF for the first time. NB you cannot use own p value the first time you run the program
                    
        smoothy = csaps(x,y,p2);  % Smoothing the ESF for the second time, this time a p value from the slider is used
        cla(handles.PlotArea,'reset') % refreshing the plot area
        plot(x,y,'r'),hold on,fnplt(smoothy),xlabel('x'),ylabel('y'),legend('Data','Smooth Data','Location','Best'),grid on % Plot the noisy signal and the smoothed signal
        
        guidata(hObject, handles); %updates the handles
elseif typec==2  %if normal logarithmic
    
        p2 = get(handles.Pvalueslide,'Value');  % Getting the value of the slider
        p2 = 10^(-log10(p2)); % Computing the logarithm of the slider value and use it as the minus power of 10
        
        set(handles.editPvalue,'String', num2str(p2)); % Show the current value on the P value text edit box
           
        [smoothy p] = csaps(x,y); % Smoothing the ESF for the first time. NB you cannot use own p value the first time you run the program
                    
        smoothy = csaps(x,y,p2); % Smoothing the ESF for the second time, this time a p value from the slider is used
        cla(handles.PlotArea,'reset') % refreshing the plot area
        plot(x,y,'r'),hold on,fnplt(smoothy),xlabel('x'),ylabel('y'),legend('Data','Smooth Data','Location','Best'),grid on % Plot the noisy signal and the smoothed signal
        
        guidata(hObject, handles); %updates the handles
    
end
smoothflag=1; % the data has been smoothed
        if smoothflag ==1
           set(handles.SaveButton,'String','Save') 
        end
% --- Executes during object creation, after setting all properties.
function Pvalueslide_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Pvalueslide (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end



function editPvalue_Callback(hObject, eventdata, handles)
% hObject    handle to editPvalue (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 editPvalue as text
%        str2double(get(hObject,'String')) returns contents of editPvalue as a double


% --- Executes during object creation, after setting all properties.
function editPvalue_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editPvalue (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 SaveButton.
function SaveButton_Callback(hObject, eventdata, handles)
% hObject    handle to SaveButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global smoothy % declaring a global value smmothESF
global smoothflag; % once the data is smoothed this flag in truned on (i.e smoothflag=1)
if smoothflag == 0
    %% do nothing
    disp('First Smooth your data')
elseif smoothflag ==1
        Oname = get(handles.Outname,'String');
        if isempty(Oname) == 1
            disp('No name variable was specified. The defualt name "smoothy" was used.')
            set(handles.Outname,'String','smoothy') % setting the default name of the saved data
            Oname='smoothy';
        end
          %smoothESF = ppval(x,smoothESF);
          assignin('base', Oname, smoothy) % assigning the new smoothed ESF to a value in the workspace

          %% after saving the gui must close

        close(gcbf)
        
end

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


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



function Outname_Callback(hObject, eventdata, handles)
% hObject    handle to Outname (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 Outname as text
%        str2double(get(hObject,'String')) returns contents of Outname as a double


% --- Executes during object creation, after setting all properties.
function Outname_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Outname (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


Contact us