Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[164e09]: frames / frame.m Maximize Restore History

Download this file

frame.m    436 lines (375 with data), 16.4 kB

  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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
function F=frame(ftype,varargin);
%FRAME Construct a new frame
% Usage: F=frame(ftype,...);
%
% `F=frame(ftype,...)` constructs a new frame object *F* of type
% *ftype*. Arguments following *ftype* are specific to the type of frame
% chosen.
%
% Time/frequency frames
% ---------------------
%
% `frame('dgt',g,a,M)` constructs a Gabor frame with window *g*,
% time-shift *a* and *M* channels. See the help on |dgt| for more
% information.
%
% `frame('dgtreal',g,a,M)` constructs a Gabor frame for real-valued
% signals with window *g*, time-shift *a* and *M* channels. See the help
% on |dgtreal| for more information.
%
% `frame('dwilt',g,M)` constructs a Wilson basis with window *g* and *M*
% channels. See the help on |dwilt| for more information.
%
% `frame('wmdct',g,M)` constructs a windowed MDCT basis with window *g*
% and *M* channels. See the help on |wmdct| for more information.
%
% `frame('filterbank',g,a,M)` constructs a filterbank with filters *g*,
% time-shifts of *a* and *M* channels. For the ease of implementation, it
% is necessary to specify *M*, even though it strictly speaking could be
% deduced from the size of the windows. See the help on |filterbank| for
% more information on the parameters. Similarly, you can construct a
% uniform filterbank by selecting `'ufilterbank'`, a positive-frequency
% filterbank by selecting `'filterbankreal'` or a uniform
% positive-frequency filterbank by selecting `'ufilterbankreal'`.
%
% `frame('nsdgt',g,a,M)` constructs a non-stationary Gabor frame with
% filters *g*, time-shifts of *a* and *M* channels. See the help on
% |nsdgt| for more information on the parameters. Similarly, you can
% construct a uniform NSDGT by selecting `'unsdgt'`, an NSDGT for
% real-valued signals only by selecting `'nsdgtreal'` or a
% uniform NSDGT for real-valued signals by selecting `'unsdgtreal'`.
%
% Pure frequency frames
% ---------------------
%
% `frame('dft')` constructs a basis where the analysis operator is the
% |dft|, and the synthesis operator is its inverse, |idft|. Completely
% similar to this, you can enter the name of any of the cosine or sine
% transforms |dcti|, |dctii|, |dctiii|, |dctiv|, |dsti|, |dstii|,
% |dstiii| or |dstiv|.
%
% `frame('dftreal')` constructs a normalized |fftreal| basis for
% real-valued signals of even length only. The basis is normalized the
% |dft| to ensure that is it orthonormal.
%
% Special / general frames
% ------------------------
%
% `frame('gen',g)` constructs an general frame with analysis matrix *g*.
% The frame atoms must be stored as column vectors in the matrices.
%
% `frame('identity')` constructs the canonical orthornormal basis, meaning
% that all operators return their input as output, so it is the dummy
% operation.
%
% `frame('randn',red,seed)` constructs a frame with redundancy *red*
% constisting of random noise generated by `randn`. The optional parameter
% *seed* specifies the initial seed for the noise generation. All frame
% vectors are normalized to have unit energy / $l^2$-norm.
%
% Container frames
% ----------------
%
% `frame('fusion',w,F1,F2,...)` constructs a fusion frame, which is
% the collection of the frames specified by *F1*, *F2*,... The vector
% *w* contains a weight for each frame. If *w* is a scalar, this weight
% will be applied to all the sub-frames.
%
% `frame('tensor',F1,F2,...)` constructs a tensor product frame, where the
% frames *F1, *F2*,... are applied along the 1st, 2nd etc. dimensions. If
% you don't want any action along a specific dimension, use the `identity`
% frame along that dimension. Any remaining dimensions in the input
% signal are left alone.
%
% Examples
% --------
%
% The following example creates a Modified Discrete Cosine Transfor frame,
% analyses an input signal and plots the frame coefficients:::
%
% F=frame('wmdct','gauss',40);
% c=frana(F,greasy);
% plotframe(F,c);
%
% See also: frana, frsyn, plotframe
if nargin<1
error('%s: Too few input parameters.',upper(mfilename));
end;
if ~ischar(ftype)
error(['%s: First agument must be a string denoting the type of ' ...
'frame.'],upper(mfilename));
end;
ftype=lower(ftype);
% True if the frame only works with real-valued input.
F.realinput=0;
% Handle the windowed transforms
switch(ftype)
case {'dgt','dwilt','wmdct','filterbank','ufilterbank',...
'nsdgt','unsdgt'}
F.g=varargin{1};
case {'dgtreal','filterbankreal','ufilterbankreal',...
'nsdgtreal','unsdgtreal'}
F.g=varargin{1};
F.realinput=1;
end;
% For parsing optional parameters to the transforms.
vargs={};
definput=struct();
%% ---- Pre-optional parameters
% Common operations to deal with the input parameters.
switch(ftype)
case {'dgt','dgtreal'}
F.a=varargin{2};
F.M=varargin{3};
vargs=varargin(4:end);
definput.keyvals.lt=[0 1];
definput.flags.phase={'freqinv','timeinv'};
case {'dwilt','wmdct'}
F.M=varargin{2};
case {'filterbank','ufilterbank','filterbankreal','ufilterbankreal'}
F.a=varargin{2};
F.M=varargin{3};
[F.a,~]=comp_filterbank_a(F.a,F.M,struct());
case {'nsdgt','unsdgt','nsdgtreal','unsdgtreal'}
F.a=varargin{2};
F.M=varargin{3};
% Sanitize 'a' and 'M'. Make M a column vector of length N,
% where N is determined from the length of 'a'
F.a=F.a(:);
F.N=numel(F.a);
F.M=bsxfun(@times,F.M(:),ones(F.N,1));
end;
[F.flags,F.kv]=ltfatarghelper({},definput,vargs);
F.type=ftype;
F.origargs=varargin;
%% ------ Post optional parameters
% Default value, works for all bases
F.red=1;
% Default value, frame works for all lengths
F.length=@(Ls) Ls;
switch(ftype)
case 'gen'
F.g=varargin{1};
F.frana=@(insig) F.g'*insig;
F.frsyn=@(insig) F.g*insig;
F.length = @(Ls) size(F.g,1);
F.red = size(F.g,2)/size(F.g,1);
case 'identity'
F.frana=@(insig) insig;
F.frsyn=@(insig) insig;
case 'dft'
F.frana=@(insig) dft(insig,[],1);
F.frsyn=@(insig) idft(insig,[],1);
case 'dcti'
F.frana=@(insig) dcti(insig,[],1);
F.frsyn=@(insig) dcti(insig,[],1);
case 'dctii'
F.frana=@(insig) dctii(insig,[],1);
F.frsyn=@(insig) dctiii(insig,[],1);
case 'dctiii'
F.frana=@(insig) dctiii(insig,[],1);
F.frsyn=@(insig) dctii(insig,[],1);
case 'dctiv'
F.frana=@(insig) dctiv(insig,[],1);
F.frsyn=@(insig) dctiv(insig,[],1);
case 'dft'
F.frana=@(insig) dft(insig,[],1);
F.frsyn=@(insig) idft(insig,[],1);
case 'dsti'
F.frana=@(insig) dsti(insig,[],1);
F.frsyn=@(insig) dsti(insig,[],1);
case 'dstii'
F.frana=@(insig) dstii(insig,[],1);
F.frsyn=@(insig) dstiii(insig,[],1);
case 'dstiii'
F.frana=@(insig) dstiii(insig,[],1);
F.frsyn=@(insig) dstii(insig,[],1);
case 'dstiv'
F.frana=@(insig) dstiv(insig,[],1);
F.frsyn=@(insig) dstiv(insig,[],1);
case 'dftreal'
F.frana=@(insig) fftreal(insig,[],1)/sqrt(size(insig,1));
F.frsyn=@(insig) ifftreal(insig,(size(insig,1)-1)*2,1)*sqrt((size(insig,1)-1)*2);
F.length=@(Ls) ceil(Ls/2)*2;
F.lengthcoef=@(Ncoef) (Ncoef-1)*2;
F.realinput=1;
case 'dgt'
F.coef2native=@(coef,s) reshape(coef,[F.M,s(1)/F.M,s(2)]);
F.native2coef=@(coef) reshape(coef,[size(coef,1)*size(coef,2),size(coef,3)]);
F.frana=@(insig) F.native2coef(comp_dgt(insig,F.g,F.a,F.M,F.kv.lt,F.flags.do_timeinv,0,0));
F.frsyn=@(insig) comp_idgt(F.coef2native(insig,size(insig)),F.g,F.a,F.kv.lt,F.flags.do_timeinv,0);
F.length=@(Ls) dgtlength(Ls,F.a,F.M,F.kv.lt);
F.red=F.M/F.a;
case 'dgtreal'
F.coef2native=@(coef,s) reshape(coef,[floor(F.M/2)+1,s(1)/(floor(F.M/ ...
2)+1),s(2)]);
F.native2coef=@(coef) reshape(coef,[size(coef,1)*size(coef,2),size(coef,3)]);
F.frana=@(insig) F.native2coef(comp_dgtreal(insig,F.g,F.a,F.M,F.kv.lt,F.flags.do_timeinv));
F.frsyn=@(insig) comp_idgtreal(F.coef2native(insig,size(insig)),F.g,F.a,F.M,F.kv.lt,F.flags.do_timeinv);
F.length=@(Ls) dgtlength(Ls,F.a,F.M,F.kv.lt);
F.red=F.M/F.a;
F.lengthcoef=@(Ncoef) Ncoef/(floor(F.M/2)+1)*F.a;
case 'dwilt'
F.coef2native=@(coef,s) reshape(coef,[2*F.M,s(1)/F.M/2,s(2)]);
F.native2coef=@(coef) reshape(coef,[size(coef,1)*size(coef,2),size(coef,3)]);
F.frana=@(insig) F.native2coef(comp_dwilt(insig,F.g,F.M));
F.frsyn=@(insig) comp_idwilt(F.coef2native(insig,size(insig)),F.g);
F.length=@(Ls) dwiltlength(Ls,F.M);
case 'wmdct'
F.coef2native=@(coef,s) reshape(coef,[F.M,s(1)/F.M,s(2)]);
F.native2coef=@(coef) reshape(coef,[size(coef,1)*size(coef,2),size(coef,3)]);
F.frana=@(insig) F.native2coef(comp_dwiltiii(insig,F.g,F.M));
F.frsyn=@(insig) comp_idwiltiii(F.coef2native(insig,size(insig)),F.g);
F.length=@(Ls) dwiltlength(Ls,F.M);
case 'filterbank'
F.red=sum(F.a(:,2)./F.a(:,1));
F.length=@(Ls) filterbanklength(Ls,F.a);
F.lengthcoef=@(Ncoef) round(Ncoef/F.red);
F.native2coef=@(coef) cell2mat(coef(:));
F.coef2native=@(coef,s) mat2cell(coef,s(1)*F.a(:,2)./F.a(:,1));
F.frana=@(insig) framenative2coef(F,comp_filterbank(insig,F.g,F.a));
F.frsyn=@(insig) ifilterbank(F.coef2native(insig,size(insig)),F.g,F.a);
case 'filterbankreal'
F.red=2*sum(F.a(:,2)./F.a(:,1));
F.length=@(Ls) filterbanklength(Ls,F.a);
F.lengthcoef=@(Ncoef) round(Ncoef/F.red*2);
F.native2coef=@(coef) cell2mat(coef(:));
F.frana=@(insig) F.native2coef(filterbank(insig,F.g,F.a));
F.frsyn=@(insig) 2*real(ifilterbank(F.coef2native(insig,size(insig)),F.g, ...
F.a));
case 'ufilterbank'
F.red=sum(F.a(:,2)./F.a(:,1));
F.length=@(Ls) filterbanklength(Ls,F.a);
F.lengthcoef=@(Ncoef) round(Ncoef/F.red);
F.coef2native=@(coef,s) reshape(coef,[s(1)/F.M,F.M,s(2)]);
F.native2coef=@(coef) reshape(coef,[size(coef,1)*size(coef,2),size(coef,3)]);
F.frana=@(insig) F.native2coef(ufilterbank(insig,F.g,F.a));
F.frsyn=@(insig) ifilterbank(F.coef2native(insig,size(insig)),F.g,F.a);
case 'ufilterbankreal'
F.red=2*sum(F.a(:,2)./F.a(:,1));
F.length=@(Ls) filterbanklength(Ls,F.a);
F.lengthcoef=@(Ncoef) round(Ncoef/F.red*2);
F.coef2native=@(coef,s) reshape(coef,[s(1)/F.M,F.M,s(2)]);
F.native2coef=@(coef) reshape(coef,[size(coef,1)*size(coef,2),size(coef,3)]);
F.frana=@(insig) F.native2coef(ufilterbank(insig,F.g,F.a));
F.frsyn=@(insig) 2*real(ifilterbank(F.coef2native(insig,size(insig)),F.g, ...
F.a));
case 'nsdgt'
F.coef2native=@(coef,s) mat2cell(coef,F.M,s(2));
F.native2coef=@(coef) cell2mat(coef(:));
F.length=@(Ncoef) sum(F.a);
F.lengthcoef=@(Ncoef) sum(F.a);
F.red=sum(F.M)/sum(F.a);
F.frana=@(insig) F.native2coef(nsdgt(insig,F.g,F.a,F.M));
F.frsyn=@(insig) insdgt(F.coef2native(insig,size(insig)),F.g,F.a);
case 'unsdgt'
F.coef2native=@(coef,s) reshape(coef,[F.M(1),s(1)/F.M(1),s(2)]);
F.native2coef=@(coef) reshape(coef,[size(coef,1)*size(coef,2),size(coef,3)]);
F.frana=@(insig) F.native2coef(unsdgt(insig,F.g,F.a,F.M));
F.frsyn=@(insig) insdgt(F.coef2native(insig,size(insig)),F.g,F.a);
F.length=@(Ncoef) sum(F.a);
F.lengthcoef=@(Ncoef) sum(F.a);
F.red=sum(F.M)/sum(F.a);
case 'nsdgtreal'
F.coef2native=@(coef,s) mat2cell(coef,floor(F.M/2)+1,s(2));
F.native2coef=@(coef) cell2mat(coef(:));
F.frana=@(insig) F.native2coef(nsdgtreal(insig,F.g,F.a,F.M));
F.frsyn=@(insig) insdgtreal(F.coef2native(insig,size(insig)),F.g,F.a,F.M);
F.length=@(Ncoef) sum(F.a);
F.lengthcoef=@(Ncoef) sum(F.a);
F.red=sum(F.M)/sum(F.a);
case 'unsdgtreal'
F.coef2native=@(coef,s) reshape(coef,floor(F.M(1)/2)+1,s(1)/ ...
(floor(F.M(1)/2)+1),s(2));
F.native2coef=@(coef) reshape(coef,[size(coef,1)*size(coef,2),size(coef,3)]);
F.frana=@(insig) F.native2coef(unsdgtreal(insig,F.g,F.a,F.M));
F.frsyn=@(insig) insdgtreal(F.coef2native(insig,size(insig)),F.g,F.a,F.M);
F.length=@(Ncoef) sum(F.a);
F.lengthcoef=@(Ncoef) sum(F.a);
F.red=sum(F.M)/sum(F.a);
case 'fusion'
F.w=varargin{1};
F.frames=varargin(2:end);
F.Nframes=numel(F.frames);
F.w=bsxfun(@times,F.w(:),ones(F.Nframes,1));
F.length = @(Ls) comp_framelength_fusion(F,Ls);
F.red=sum(cellfun(@framered,F.frames));
% These definitions binds F itself, so they must execute last
F.frana=@(insig) comp_frana_fusion(F,insig);
F.frsyn=@(insig) comp_frsyn_fusion(F,insig);
case 'tensor'
% This frame type is currently broken. It must be reworked to reshape
% to the standard layout in order not to break all the assumptions.
F.frames=varargin;
F.Nframes=numel(F.frames);
for ii=1:F.Nframes
if F.frames{ii}.realinput
error(['It is not safe to embed a real-valued-input-only frame ' ...
'into the tensor frame.']);
end;
end;
F.frana=@(insig) comp_frana_tensor(F,insig);
F.frsyn=@(insig) comp_frsyn_tensor(F,insig);
F.length=@(Ls) comp_framelength_tensor(F,Ls);
F.red=prod(cellfun(@framered,F.frames));
case {'fwt'}
F.J=varargin{2};
F.g=fwtinit({'strict',varargin{1}});
F.red= 1/(F.g.a(1)^(F.J)) + sum(1./(F.g.a(1).^(0:F.J-1))*sum(1./F.g.a(2:end)));
F.frana=@(insig) fwt(insig,F.g,F.J);
F.frsyn=@(insig) ifwt(insig,F.g,F.J,size(insig,1)/F.red);
F.length=@(Ls) fwtlength(Ls,F.g,F.J);
case {'wfbt'}
F.g=wfbtinit({'strict',varargin{1}});
F.red = sum(1./treeSub(F.g));
F.wtPath = nodesBForder(F.g);
F.rangeLoc = rangeInLocalOutputs(F.wtPath,F.g);
F.rangeOut = rangeInOutputs(F.wtPath,F.g);
F.coef2native = @(coef,s) wavpack2cell(coef,wfbtclength(s(1)/F.red,F.g));
F.native2coef = @(coef) wavcell2pack(coef);
F.frana=@(insig) F.native2coef(comp_wfbt(insig,F.g.nodes(F.wtPath),F.rangeLoc,F.rangeOut,'per'));
F.frsyn=@(insig) iwfbt(F.coef2native(insig,size(insig)),F.g,size(insig,1)/F.red);
F.length=@(Ls) wfbtlength(Ls,F.g);
case {'wpfbt'}
F.g=wfbtinit({'strict',varargin{1}});
F.red = sum(cellfun(@(aEl) sum(1./aEl),nodeSub(nodesBForder(F.g),F.g)));
F.coef2native = @(coef,s) wavpack2cell(coef,...
s(1)./cell2mat(cellfun(@(aEl) aEl(:),...
reshape(nodeSub(nodesBForder(F.g),F.g),[],1),'UniformOutput',0))./F.red);
F.native2coef = @(coef) wavcell2pack(coef);
F.frana=@(insig) F.native2coef(wpfbt(insig,F.g));
F.frsyn=@(insig) iwpfbt(F.coef2native(insig,size(insig)),F.g,size(insig,1)/F.red);
F.length=@(Ls) wfbtlength(Ls,F.g);
case {'ufwt'}
F.J=varargin{2};
F.g=fwtinit({'strict',varargin{1}});
F.coef2native = @(coef,s) reshape(coef,[s(1)/(F.J*(numel(F.g.a)-1)+1),F.J*(numel(F.g.a)-1)+1,s(2)]);
F.native2coef = @(coef) reshape(coef,[size(coef,1)*size(coef,2),size(coef,3)]);
F.frana=@(insig) F.native2coef(ufwt(insig,F.g,F.J));
F.frsyn=@(insig) iufwt(F.coef2native(insig,size(insig)),F.g,F.J);
F.length=@(Ls) Ls;
F.red=(F.J*(numel(F.g.a)-1)+1);
case {'uwfbt'}
F.g=wfbtinit({'strict',varargin{1}});
F.red = noOfOutputs(F.g);
F.coef2native = @(coef,s) reshape(coef,[s(1)/F.red,F.red,s(2)]);
F.native2coef = @(coef) reshape(coef,[size(coef,1)*size(coef,2),size(coef,3)]);
F.frana=@(insig) F.native2coef(uwfbt(insig,F.g));
F.frsyn=@(insig) iuwfbt(F.coef2native(insig,size(insig)),F.g);
F.length=@(Ls) Ls;
case {'uwpfbt'}
F.g= wfbtinit({'strict',varargin{1}});
F.red = sum(cellfun(@(fEl) numel(fEl.g),F.g.nodes));
F.coef2native = @(coef,s) reshape(coef,[s(1)/F.red,F.red,s(2)]);
F.native2coef = @(coef) reshape(coef,[size(coef,1)*size(coef,2),size(coef,3)]);
F.frana=@(insig) F.native2coef(uwpfbt(insig,F.g));
F.frsyn=@(insig) iuwpfbt(F.coef2native(insig,size(insig)),F.g);
F.length=@(Ls) Ls;
otherwise
error('%s: Unknown frame type: %s',upper(mfilename),ftype);
end;
% This one is placed at the end, to allow for F.red to be defined
% first.
if ~isfield(F,'lengthcoef')
F.lengthcoef=@(Ncoef) Ncoef/framered(F);
end;