Monday 16 January 2012

scilab:maximal-minimal

先ほどのを使ってサクっと書いてみた,任意のベクトルの,極小・極大点を返す関数.
初めて書いた引数をとる関数でもある.
ベクトルは行でも列でもよくしてあり(関数内でリサイズするようにした),
最後に勝手に plot する事もできる機能もつけてみた.
結果は,元データの極大・極小を取るところの添字をそれぞれ maxes, mins という global variable に格納する.
音声データなどの,がたがたしたデータに対して,全体の挙動を捉えるのに使えるのでは,と思っている.
複数回使えばなお良いかもしれない.一番上とか下のとこなぞっていく感じでね.

ではソースコード.
16th January 2012 copyright mutsuteru 
(the author of the blog,http://lesguillemets.blogspot.com/ ,
and the one who is tweeting @exumbra_insolem)
all rights reserved.
Published for personal use only.
Sharing any outcome using this program or programs based on this
without explicit permission from the author is prohibited.

//next function finds the max or min of the vector.
//returns the index of the max or min.
//uses mmplus,mmf,mmminus,mmdif as local variable,and
//returns maxes and mins as global vars.
global maxes mins;
function maxminfinder(mmf)
    global maxes mins;
    mmf=matrix(mmf,-1,1);
    mmplus=[0;mmf];
    mmplus($)=[];
    mmminus=[mmf;0];
    mmminus(1)=[];
    maxes=find(mmf-mmminus>=0 & mmplus-mmf<0);
    mins=find(mmf-mmminus<0 & mmplus-mmf>=0);
    a=input("PLOT? INPUT 1 TO PLOT.")
    if a==1 then plot(maxes,mmf(maxes)); plot(mins,mmf(mins));
    end
endfunction
行列のお陰ですっきりしたソースになったかも.
可読性は改善の余地あり.

実際使ってみるとこういう感じになる.
もとのうねうねはテスト用に作った行列で,
それについて極小・極大を取る点を
それぞれ結んだ図.
wavreader とかと組み合わせるのもアリ.

No comments:

Post a Comment