[Scilab-users] find and locate local maxima

Federico Miyara fmiyara at fceia.unr.edu.ar
Tue Mar 16 20:17:11 CET 2021


David:

Just in case it is useful for your purpose, I'm attaching a flexible 
localmax function.

To filter out noisy signals you could use a low-pass filter or 
lsq_splin() which can also remove multivalued data from replicated 
experiments.

Regards,

Federico Miyara

On 16/03/2021 10:52, CHEZE David 227480 wrote:
>
> Hi all,
>
> I’m looking for function that could find and locate every local maxima 
> of any discrete time signal (timeseries), similar to Matlab or Octave 
> function findpeaks(), scipy find_peaks(). Is anyone aware if something 
> similar is already available in Scilab ? (I already browsed a little 
> bit and it don’t seem so…)
>
> If not in Scilab macros, any hint to use the Octave or scipy function 
> directly from Scilab?
>
> More globally it seems that Octave Forge could be linked with Python 
> (from oct2py import octave
>
> # Load the Octage-Forge signal package.
>
> octave.eval("pkg load signal")), does someone ever tried to bridge 
> similarly in Scilab ? oct2sci
>
> Kind regards,
>
> David
>
>
> _______________________________________________
> users mailing list
> users at lists.scilab.org
> http://lists.scilab.org/mailman/listinfo/users



-- 
El software de antivirus Avast ha analizado este correo electrónico en busca de virus.
https://www.avast.com/antivirus
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20210316/a0229751/attachment.htm>
-------------- next part --------------
function [i, y] = localmax(x, s)
    // This function finds local maxima within vector x
    //
    // Usage:
    //        [i, y] = localmax(x, s) 
    // 
    // where  x: input vector
    //        s: strict (2), semistrict (1) or loose (0) local maxima
    //        i: vector of indexes where x reaches local maxima
    //        y: vector of values of x at such indexes   
    //
    // Local maxima are defined as values greater than or equal to the
    // immediately preceding and immediately following ones. Argument 
    // s is optional. If it is equal to 2, strict maxima are found 
    // (i.e., the values are strictly greater than the neighbouring 
    // ones); if it is equal to 1, semistrict maxima are found (i.e.,
    // the values are strictly greater than at least one neighbouring
    // value); if it is equal to 0, loose maxima are found (i.e., the
    // values are greater than or equal to the neighbouring ones). x 
    // may be either a row vector or a column vector. If x is complex,
    // abs(x) is used instead of x and given as the second output 
    // argument.
    // 
    // NOTE: In order to assess the first and last elements the vector
    //       x is extended with -inf values 
    //
    // ------------------------------
    // Author: Federico Miyara
    // Dete:   2007-06-28
    //         2008-08-09
    //         2010-06-28
    //         2010-08-24
    //         2020-01-24 
    //         2020-04-22      

    // Default value for s (loose maxima)
    if argn(2)<2
        s = 0;
    end

    // Ensure x is a column vector
    if size(x,1)==1
        x = x(:);
        wasrow = 1;
    else 
        wasrow = 0;
    end

    // For complex signals, the maximum will be obtained for
    // the absolute value 
    if isreal(x)==0
        x = abs(x);
    end

    // Compute a vector xdec that is positive on indexes where x
    // is decreasing. To that end, the next value is subtracted
    // from each value of x
    xdec = x - [x(2:$); -%inf];

    // Compute a vector xinc that is positive on indexes where x
    // is increasing. To that end, the preceding value is subtracted
    // from each value of x
    xinc = x - [-%inf; x(1:$-1)];

    // Obtain a vector whose components are 1 if both xdec and xinc
    // are positive or non-negative according to s.
    switch s
    case 2
        maxi = (xdec>0).*(xinc>0);
    case 1
        maxi =  (xdec>=0).*(xinc>0) + (xdec>0).*(xinc>=0);
    case 0
        maxi = (xdec>=0).*(xinc>=0);
    end

    // Find the indexes corresponding to nonzeros
    i = find(maxi); 

    // If x was a row vector, so must be i
    if wasrow== 1
        i = i(:).';
    else
        i = i(:);
    end

    // Provide the second output argument if requested 
    if argn(1)>1
        y = x(i);
        // If x was a row vector, so must be y
        if wasrow==1
            y = y(:).';
        else
            y = y(:);
        end
    end

endfunction


More information about the users mailing list