%
%  signal_functions_stats.m 
%  ver 1.3  August 2, 2007 
%  by Tom Irvine  Email: tomirvine@aol.com
%
function [n] = signal_function_stats(tim,amp)
%
        n = length(amp);
        mu=mean(amp);
        sd=std(amp);
        mx=max(amp);
        mi=min(amp);
        rms=sqrt(sd^2+mu^2);
        kt=0.;
    %1
        dt_max=0;
        dt_min=1.0e+90;
        for i=1:n    
            if( amp(i)==mx)
                tt_max=tim(i);
            end
            if( amp(i)==mi)
                tt_min=tim(i);
            end
            kt=kt+amp(i)^4;
%
            if(i>1)
                ldt=tim(i)-tim(i-1);
                if(ldt>dt_max)
                    dt_max=ldt;
                end
                if(ldt<dt_min)
                    dt_min=ldt;
                end       
            end
%
        end      
        kt=kt/(n*sd^4);
    %1
        disp(' ')
        disp(' time stats ')
        disp(' ')
        tmx=max(tim);
        tmi=min(tim);
    % disp(out0)
        out3 = sprintf(' start  = %g sec    end = %g sec            ',tmi,tmx);
        disp(out3)     
        dt=(tmx-tmi)/(n-1);
%
        disp(' ')
        out10 = sprintf('  maximum dt = %8.4g sec ',dt_max);
        out11 = sprintf('  overall dt = %8.4g sec ',dt);
        out12 = sprintf('  minumim dt = %8.4g sec ',dt_min);
%
        disp(out10);
        disp(out11);
        disp(out12);
%
        disp(' ')
        out10 = sprintf('  maximum sr = %8.4g samples/sec ',1/dt_min);
        out11 = sprintf('  overall sr = %8.4g samples/sec ',1/dt);
        out12 = sprintf('  minumim sr = %8.4g samples/sec ',1/dt_max);
%
        disp(out10);
        disp(out11);
        disp(out12);        
%        
        sr=1/dt;
        out4 = sprintf(' SR  = %8.4g samples/sec ',sr); 
        out5 = sprintf('\n number of samples = %d  ',n);
%        disp(out4)
        disp(out5)
        disp(' ')
        disp(' amplitude stats ')
        disp(' ')
        out0 = sprintf(' number of points = %d ',n);
        out1 = sprintf(' mean = %8.4g    std = %8.4g    rms = %8.4g \n',mu,sd,rms);
        out2a = sprintf(' max  = %9.4g  at  = %8.4g sec            ',mx,tt_max);
        out2b = sprintf(' min  = %9.4g  at  = %8.4g sec            \n',mi,tt_min);
    %
        amax=abs(mx);
        amin=abs(mi);
        if(amax<amin)
            amax=amin;
        end
        crest=amax/rms;
        out4 =  sprintf(' crest factor = %8.4g    kurtosis  = %8.4g \n',crest,kt);
    %
%        out5  = sprintf(' kurtosis  = %8.4g \n',kt);
    %
        nzc=0;
        pzc=0;
        for(i=1:n-1)
%
            if(amp(i) <=0 & amp(i+1) > 0)
                pzc=pzc+1;
            end
%       
	        if(amp(i) >=0 & amp(i+1) < 0)
                nzc=nzc+1;      
            end 
        end
%
        out6=sprintf(' zero crossings:  positive = %d   negative = %d \n',pzc,nzc);
%
        j=1;
%
        np=0;
        pp=0;
%
        for(i=2:(n-1))
            if( amp(i)>=amp(i-1) & amp(i)>=amp(i+1) & amp(i)>0 )
                pa(j)=abs(amp(i));
                j=j+1;
                pp=pp+1;
            end
            if( amp(i)<=amp(i-1) & amp(i)<=amp(i+1)& amp(i)<0 )
                pa(j)=abs(amp(i));
                j=j+1;
                np=np+1;
            end
        end
%
        out9 =sprintf(' number of positive peaks = %d ',pp);
        out10=sprintf(' number of negative peaks = %d \n',np);
%
        disp(out1)
        disp(out2a)
        disp(out2b)
        disp(out4)
%        disp(out5)
        disp(out6)
        disp(out9)
        disp(out10)
        disp(' Plot time history file? ')
        choice = input(' 1=yes 2=no ');
        if(choice == 1)
            figure(1);
            plot(tim,amp)
            xlabel(' Time (sec)');
            zoom on;
        end
%
        disp(' ')
        disp(' Plot histogram? ')
        choice = input(' 1=yes 2=no ');
%
        if(choice == 1)
            figure(2);    
            hist(amp,11)
            title(' Histogram ');
            ylabel(' Counts ');
        end
%
        disp(' ')
        disp(' Plot absolute value peak distribution? ')
        choice = input(' 1=yes 2=no ');
%
        if(choice==1)
            figure(3);  
            hist(pa,15)
            title(' Histogram of Absolute Values of Peaks');
            ylabel(' Counts ');
        end