summaryrefslogtreecommitdiff
path: root/macros/movingrms.sci
blob: 07e6f87c13d6104a488c79695b320d071fb9126d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
function [rmsx, w] =  movingrms(x, width, risetime, varargin)

// Find moving RMS value of signal in x

// Calling Sequence
//[rmsx,w]=movingrms(x,width,risetime)
//[rmsx,w]=movingrms(x,width,risetime,Fs)

// Parameters
//	x: (Real or complex valued vector or matrix) Input Signal
//	width: Real or complex scalar value
//	risetime: Real or complex scalar value
//	Fs: (Real or complex scalar value) Sampling frequency

// Description
//	In this function signal is convoluted against a sigmoid window of width w and risetime rc with the units of these parameters relative to the value of the sampling frequency given in Fs (Default value=1).

// Example : 1
//[a,b]=movingrms ([4.4 94 1;-2 5*%i 5],1,-2)
// Output:
// b  =
//
//    0.1887703
//    0.1887703
// a  =
//
//    0.9123683    17.719291    0.9625436
//    0.9123683    17.719291    0.9625436

//Example : 2
// [a,b]=movingrms ([4.4 94 1;-2 5*%i 5],1,-2,2)
//Output :
// b  =
//
//    1.
//    1.
// a  =
//
//    4.8332184    93.866927    5.0990195
//    4.8332184    93.866927    5.0990195

funcprot(0);

//**************************************************************************************************
//______________________________________________version1 code (not working)_________________________
//__________________________________________________________________________________________________
//**************************************************************************************************

//rhs=argn(2);
//if (rhs<3) then
//	error("Wrong number of input arguments.")
//elseif (rhs==3) then Fs=1;
//end
//[rmsx,w]=callOctave("movingrms",x,w,rc,Fs)

//**************************************************************************************************
//______________________________________________version2 code ( working)____________________________
//__________________________________________________________________________________________________
//**************************************************************************************************
if  argn(2) > 4 | argn(2) < 3 then
    error("movingrms : wrong number of inputs ")
end

    if length(varargin)==0 then
        Fs = 1;
    else
        Fs = varargin(1);
    end

  [N nc] = size (x);
  if width*Fs > N/2
    idx = [1 N];
    w    = ones(N,1);
  else
    idx   = round ((N + width*Fs*[-1 1])/2);
    w     = sigmoid_train((1:N)', idx, risetime*Fs);
  end
  fw    = fft (w.^2);
  //fx    = fft (x.^2); itdoes columwise fft in Octave but in scilab it does 2D fft

  //Evaluating columnwise fft using for loop
     fx = [];
  for k = 1:nc
      fx = [fx fft(x(:,k).^2)];
      k = k + 1;
  end
 //end of Evaluating columnwise fft using for loop

 // in octave fx.*fw  does row element wise multiplication but it is inconsistance in scilab
 //doing it using for loop
 fxw = [];
 for k = 1:N
     fxw = [fxw ; fx(k,:).*fw(k,:)]
 end
 //end of fxw computation

  //rmsx  = real(ifft (fxw)/(N-1)); itdoes columwise ifft in Octave but in scilab it does 2D ifft
    //Evaluating columnwise ifft using for loop
     ifxw = [];
  for k = 1:nc
      ifxw = [ifxw ifft(fxw(:,k))];
      k = k + 1;
  end

  rmsx  = real(ifxw/(N-1))
 //end of Evaluating columnwise ifft using for loop

  rmsx (rmsx < %eps*max(rmsx(:))) = 0;

  rmsx  = circshift (sqrt (rmsx), round(mean(idx)));
  //##w     = circshift (w, -idx(1));

endfunction