Peak finder in matlab

Here is some simple code which finds max(),min() peak coordinates and display it in matlab visual plot
function peak_finder(t,y)
%**************************************************************************
% Name  : www.cpfaq.blogspot.com
% Sub   : To find a maximum peak and a minimm peak in a plot 
% Input : x axis values ,y axis values
% Modified By  :
% © 2010-2011 Copyright www.cpfaq.blogspot.com.
%*************************************************************************
            % Error checking(Input)
            % Number of arguments check
            if nargin~=2
                errordlg('Enter Valid input arguments','peak_finder');
                  return;  % Returns from function 
            end
            % Length checking
            % Find the length of x axis values
            len_x=length(t);
            % Find the length of y axis values
            len_y=length(y);
            % Check out for error
            if len_x~=len_y
                errordlg('Length of both the variablers should be same','peak_finder');
                return;   % Returns from funtion
            end
            % End of error checking 
            % Find the maximum of y and its corresponding place 
            [y1,it1]=max(y);
            % To plot in a same figure          
            hold on;
            % Calculate the minimum value of y and its corresponding place
            [y2,it2]=min(y);
            % Plot the maximum value with graphics
            plot(t(it1),y1,'^', 'linewidth',1,...
                                'MarkerEdgeColor','k',...
                                'MarkerFaceColor','blue',...
                                'MarkerSize',10);
            % Plot the minimum value with graphics  
            plot(t(it2),y2,'^', 'linewidth',1,...
                                'MarkerEdgeColor','k',...
                                'MarkerFaceColor','g',...
                                'MarkerSize',10);
            % Add text to the max peak value
            text(t(it1),y1,sprintf('   R(%4.2f,%4.2f)',t(it1),y1),'color',[.09 .84 .38],'fontsize',10); 
            % Add text to the min peak value
            text(t(it2),y2,sprintf('   S(%4.2f,%4.2f)',t(it2),y2),'color',[.09 .84 .38],'fontsize',10);
            % Hold the figure in to off condition
            hold off;
            % End Of File
end    % End of funtion peak_finder
%************************************************************************** 

If you find this code useful .. and plz send 
the screenshots of matlab plot output to offshore_support@in.com .
#include <stdio.h>
#include <stdlib.h>

#pragma warning (disable:4305)

typedef struct peakCoordinates
{
    double xCoord;
    double yCoord;
} peakXYcoord;

/*********************************************************************************
*               ^
*               *
*          5.8  *                ^ Peak(2.3,5.8)
*               *                *
*               *               * *
*               *   *          *    *
*   (Y axis     * *   *       *       *
*    values)    **      *    *          * *
*   say magtude *        *   *              * * * *
*               *         * *
*             ************************************* >
*               *    X axis values ( say Time (in sec)
*                                (2.3)
*
***********************************************************************************/

/* Function Prototypes */
peakXYcoord *peakFinder(double xDataArr[],double yDataArr[],int len);
int findMaxLocation(double xDataArr[],int len);
/* End of function prototypes */

int main(int argc,char *argv[])
{
    peakXYcoord *peaks;
    int lenX,lenY;
    double xData[] = {1.0,2.0,13.0,8.0,22.0,13.4};         //Init with Raw data
    double yData[] = {3.0,2.0,4.0,0.0,1.0,12.0}; //Init with Raw data
    int len = (lenX = sizeof(xData)/sizeof(xData[0])) > (lenY = sizeof(yData)/sizeof(yData[0]))?lenX:lenY;
    peaks = peakFinder(xData,yData,len);
    printf("Peaks(%f,%f)\n",peaks->xCoord,peaks->yCoord);
    free(peaks);
    return(getchar());
}

peakXYcoord *peakFinder(double xDataArr[],double yDataArr[],int len)
{
    peakXYcoord *peakCoord =(peakXYcoord *) malloc(sizeof(peakXYcoord));
    peakCoord->yCoord = yDataArr[findMaxLocation(yDataArr,len)];
    peakCoord->xCoord = xDataArr[findMaxLocation(yDataArr,len)]; //see why yDataArr ?
    return peakCoord;
}

int findMaxLocation(double DataArr[],int len)
{
    int i = 0,j = 1,loc = 0;
    for(i = 0; i < len ; i++)
    {
        for(j = i+1 ; j < len ; j++)
        {
            if(DataArr[j] > DataArr[i])
            {
                loc = j;
            }
        }
    }
    return loc;
}
If you find this code useful .. and plz send 
the screenshots of matlab plot output to offshore_support@in.com .

No comments: