summaryrefslogtreecommitdiff
path: root/macros/rootmusic.sci
blob: 34c3ff6af5949ad2fa9ee66a5ac03e65d964f5ec (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// Date of creation: 20 Jan, 2016
function [w,pow] = rootmusic(x,p,varargin)
    // Frequencies and power of sinusoids using the root MUSIC algorithm
    //
    // Calling Sequence
    // w = rootmusic(x,p)
    // [w,pow] = rootmusic(x,p)
    // [f,pow] = rootmusc(...,fs)
    // [w,pow] = rootmusic(...,'corr')
    //
    // Parameters
    // x - int|double - vector|matrix
    //      Input signal.
    //      If x is a vector, then it reprsenets one realization of the signal.
    //      If x is a matrix, then each row represents a separate observation of
    //      the signal. 
    // p - int|double - scalar|2 element vector
    //      p(1) is the signal subspace dimension and hence the number of 
    //      complex exponentials in x.
    //      p(2), if specified, represents a threshold that is multiplied by 
    //      the smallest estimated eigenvalue of the signal's correlation 
    //      matrix.
    // fs - int|double - scalar
    //      Sampling frequency (in Hz)
    //      If fs is specified by an empty vector or unspecified, it defaults
    //      to 1 Hz
    // 'corr' flag
    //      If specified, x is interpreted as a correlation matrix rather than
    //      a matrix of the signal data. For x to be a correlation matrix, 
    //      x must be a square matrix and all its eigenvalues must be 
    //      nonnegative
    //
    // Examples:
    //      1) 3 complex exponentials:
    //
    //          n=0:99;   
    //          s=exp(1i*pi/2*n)+2*exp(1i*pi/4*n)+exp(1i*pi/3*n)+randn(1,100);  
    //          [W,P] = rootmusic(s,3);
    //
    // Author
    // Ayush
    //
    // See also
    // corrmtx | peig | pmusic | rooteig
    //
    // References
    // 1) Monson H. Hayes, Statistical Digital Signal Processing And Modeling, 
    // Wiley & Sons, Inc, [Section 8.6.3]
    //
    //
    // Output arguments
    // w - double - vector
    //      Estimated frequencies of the complex sinusoids
    // pow - double - vector
    //      estimated absolute value squared amplitudes of the sinusoids at 
    //      the frequencies w 
    //
    
    funcprot(0);
    
    exec('musicBase.sci',-1);
    exec('nnls.sci',-1);  
    
    
    // **** checking the number of input and output arguments ****
    
    [numOutArgs, numInArgs] = argn(0);
    
    if numOutArgs~=1 & numOutArgs~=2 then
        error(78,"rootmusic");
    end
    
    if numInArgs<1 | numInArgs>4 then
        error(77,"rootmusic");
    end
    
    
    // **** parsing the input arguments ****
    isFsSpecified = %F;
    fs = [];
    
    varargLength = length(varargin);
    // searching for the 'corr' flag
    isCorrFlag = %F;
    
    if varargLength==0 then
        stringIndices = [];
    else
        stringIndices = find(type(varargin(1:varargLength))==10);
    end
    
    if ~isempty(stringIndices) then
        // ignoring all other strings except the corr flag
        isCorrFlag = or(strcmpi(varargin(stringIndices),"corr")==0);
        varargin(stringIndices) = [];
    end
    
    // varargin can have only an entry for fs
    if length(varargin)==1 then
        fs = varargin(1);
        if length(fs)==1 then
            if ~IsIntOrDouble(fs, %T) then
                msg = "rootmusic: Wrong type for argument #4 (fs); Positive scalar expected";
                error(msg,10084);
            end
            fs = double(fs);
            isFsSpecified = %T;
        elseif length(fs)>1 then
            msg = "rootmusic: Wrong type for argument #4 (fs); Positive scalar expected";
            error(msg,10084); 
        end
    elseif length(varargin)>1 then
        msg = "rootmusic: Wrong type for argument #4 (fs); Positive scalar expected";
        error(msg,10084);     
    end
    
    // extracting primary input x/R
    primaryInput = x;

    if ndims(primaryInput)<1 | ndims(primaryInput)>2 then
        msg = "rootmusic: Wrong dimension for argument #1; Vector or a matrix expected";
        error(msg,10053);
    end
    if ~IsIntOrDouble(primaryInput, %F) then
        msg = "rootmusic: Wrong type for argument #1; Numeric vector or a matrix expected";
        error(msg,10053);
    end
    // covert to a column vector
    if ndims(primaryInput)==1 then
        primaryInput = primaryInput(:);
    end
    // casting to double
    primaryInput = double(primaryInput);


    //****extracting p****
    // p must be either scalar or a 2-element vector
    if length(p)~=1 & length(p)~=2 then
        msg = "rootmusic: Wrong type for argument #2 (p); " + ...
            "A scalar or a 2-element vector expected";
        error(msg,10053);
    end
    // first argument of p must be an integer
    if ~IsIntOrDouble(p(1),%T) then
        msg = "rootmusic: Wrong input argument #2 p(1); " + ... 
            "positive integer expected";
        error(msg,10036);
        return
    end
    p(1) = int(p(1));
    // TODO: check if positive required
    // 2nd argument, if exists, must be a positive integer'
    if length(p)==2 then
        if ~IsIntOrDouble(p(2),%F) then
            msg = "rootmusic: Wrong type for argument #2 p(2); must be a scalar";
            error(msg,10053); 
        end
    end
    
    isXReal = isreal(x)
    if ~isCorrFlag then
        // check that p(1) should be even if x is real
        if isXReal & modulo(p(1),2)~=0 then
            msg = "rootmusic: Wrong input argument #2 p(1); " + ...
                " An even value expected for real input x";
            error(msg,10036);
        end
    end

    
    
    // **** calling pmusic ****
    data= struct();
    data.x = primaryInput;
    data.p = p;
    data.nfft = 256;
    data.w = [];
    data.fs = fs;
    data.isWindowSpecified = %F;
    data.windowLength = 2*p(1);
    data.windowVector = [];
    data.noverlap = [];
    data.isCorrFlag = isCorrFlag;
    data.isFsSpecified = isFsSpecified;
    data.freqrange = "twosided";

    

    [outData,msg] = musicBase(data);
    if length(msg)~=0 then
        // throw error
        msg = "rootmusic: "+msg
        error(msg);
    end
    
    pEffective = outData.pEffective;
    eigenvals = outData.eigenvals;
    
    w = computeFreqs(outData.noiseEigenvects,pEffective,%f,eigenvals);
    
    if isempty(w) then
        // assign all frequency and powers as -nan
        w = %nan*(1:pEffective)';
        pow = w;
        return;
    end
         
    
    // **** Estimating the variance of the noise ****
    // Estimate is the mean of the eigenvalues belonging to the noise subspace
    sigma_noise = mean(eigenvals(pEffective+1:$));
    
    pow = computePower(outData.signalEigenvects,eigenvals,w,pEffective,...
                sigma_noise,isXReal);
                
                
    // is fs is specified, convert normailized frequencies to actual frequencies
    if isFsSpecified then
        w = w*fs/(2*%pi);
    end
    
    
endfunction

function w = computeFreqs(noiseEigenvects,pEffective,EVFlag,eigenvals)
    // Computes the frequencies of the complex sinusoids using the roots of 
    // the polynomial formed with the noise eigenvectors
    //
    // Parameters
    // noiseEigenvects - 
    //      A matrix where noise eigenvectors are represented by each column
    // pEffective - 
    //      The effective dimension of the signal subspace
    // EVFlag - 
    //      Flag to indicate weighting to be used for rooteig
    // eigenvals - 
    //      Eigenvals of the correlation matrix
    //
    // Output arguments
    //  w - 
    //      A vector with frequencies of the complex sinusoids


    numOfNoiseEigenvects = size(noiseEigenvects,2);
    if EVFlag then
        // weights are the eigenvalues in the noise subspace
        weights = eigenvals($-numOfNoiseEigenvects+1:$);
    else
        weights = ones(numOfNoiseEigenvects,1);
    end
    
    // Form a polynomial consisting of a sum of polynomials given by the
    // product of the noise subspace eigenvectors and the reversed and 
    // conjugated version. (eq 8.163 from [1])
    D = 0;
    for i=1:numOfNoiseEigenvects
        eigenvect = noiseEigenvects(:,i);
        D = D + conv(eigenvect,conj(eigenvect($:-1:1)))./weights(i);
    end
    
    roots = roots(D);
    
    // selecting the roots inside the unit circle
    rootsSelected = roots(abs(roots)<1);
    
    // sort the roots in order of increasing distance from the unit circle
    [dist,indices] = gsort(abs(rootsSelected)-1);
    
    sortedRoots = rootsSelected(indices);
    
    if isempty(sortedRoots) then
        w = [];
    else
        w = atan(imag(sortedRoots(1:pEffective)),real(sortedRoots(1:pEffective)));
    end
    
    
endfunction


function power = computePower(signalEigenvects,eigenvals,w,pEffective,...
                    sigma_noise,isXReal)
                    
    if isXReal then
        // removing the negative frequencies as sinusoids will be present in 
        //  complex conjugate pairs
        w = w(w>=0);
        pEffective = length(w);
    end
    
    // Solving eq. 8.160 from [1] (Ap = b) where p is the power matrix
    
    A = zeros(length(w),pEffective);
    
    for i=1:pEffective
        A(:,i) = computeFreqResponseByPolyEval(signalEigenvects(:,i), ...
                        w,1,%F);
    end
    
    A = (abs(A).^2)';
    b = eigenvals(1:pEffective) - sigma_noise;
    
    // Solving Ap=b with the constraint that all elements of p >=0
    power = nnls(A,b+A*sqrt(%eps)*ones(pEffective,1));
    
    
endfunction

function h = computeFreqResponseByPolyEval(b,f,fs,isFsSpecified)
    // returns the frequency response (h) for a digital filter with numerator b.
    // The evaluation of the frequency response is done at frequency values f
    
    f = f(:);
    b = b(:);
    if isFsSpecified then
        // normalizing the f vector
        w = f*2*%pi/fs;
    else
        w = f;
    end
    
    n = length(b);
    powerMatrix = zeros(length(f),n);
    powerMatrix(:,1) = 1;
    for i=2:n
        powerMatrix(:,i) = exp(w*(-i+1)*%i);
    end
    
    h = powerMatrix*b;
     
endfunction    
    

function result = IsIntOrDouble(inputNum, isPositiveCheck)
    // Checks if The Input Is Integer Or Double
    // Also Checks if It Is Greater Than 0 For IsPositiveCheck = True

    if ~(type(inputNum)==1 | type(inputNum)==8) then
        result = %F;
        return
    end
    if isPositiveCheck & or(inputNum<=0) then
        result = %F;
        return
    end

    result = %T;
    return
endfunction