comparison toolboxes/MIRtoolbox1.3.2/MIRToolbox/mirframe.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function [f x] = mirframe(x,varargin)
2 % f = mirframe(x) creates the frame decomposition of the audio signal x.
3 % (x can be a file name as well.)
4 % Optional arguments:
5 % mirframe(x,'Length',w,wu):
6 % w is the length of the window in seconds (default: .05 seconds)
7 % u is the unit, either
8 % 's' (seconds, default unit)
9 % or 'sp' (number of samples)
10 % mirframe(x,'Hop',h,hu):
11 % h is the hop factor, or distance between successive frames
12 % (default: half overlapping: each frame begins at the middle
13 % of the previous frame)
14 % u is the unit, either
15 % '/1' (ratio with respect to the frame length, default unit)
16 % '%' (ratio as percentage)
17 % 's' (seconds)
18 % 'sp' (number of samples)
19 % or 'Hz' (hertz), i.e., number of frames per second: the
20 % exactness of the frame rate is ensured and may cause a
21 % slight fluctuation of the elementary hop distances.
22 % These arguments can also be written as follows:
23 % mirframe(x,w,wu,h,hu)
24 % (where some of these parameters can be omitted).
25
26 if isempty(x)
27 f = {};
28 return
29 end
30 if iscell(x)
31 x = x{1};
32 end
33 if nargin == 0
34 f = miraudio;
35 elseif isa(x,'mirdesign')
36 if not(get(x,'Eval'))
37 % During bottom-up construction of the general design
38
39 para = scanargin(varargin);
40 type = get(x,'Type');
41 f = mirdesign(@mirframe,x,para,{},struct,type);
42
43 fl = get(x,'FrameLength');
44 fh = get(x,'FrameHop');
45 flu = get(x,'FrameLengthUnit');
46 fhu = get(x,'FrameHopUnit');
47 if fl
48 f = set(f,'FrameLength',fl,'FrameLengthUnit',flu,...
49 'FrameHop',fh,'FrameHopUnit',fhu);
50 else
51 f = set(f,'FrameLength',para.wlength.val,...
52 'FrameLengthUnit',para.wlength.unit,...
53 'FrameHop',para.hop.val,...
54 'FrameHopUnit',para.hop.unit);
55 end
56 f = set(f,'FrameEval',1,...
57 'SeparateChannels',get(x,'SeparateChannels'));
58 if not(isamir(x,'miraudio'))
59 f = set(f,'NoChunk',1);
60 end
61 else
62 % During top-down evaluation initiation
63
64 if isstruct(varargin{1}) && isfield(varargin{1},'struct')
65 tmp = varargin{1}.struct;
66 x = set(x,'Struct',tmp);
67 varargin{1} = rmfield(varargin{1},'struct');
68 end
69 e = evaleach(x);
70 if iscell(e)
71 e = e{1};
72 end
73 if isempty(mirgetdata(e))
74 f = e;
75 else
76 f = mirframe(e,varargin{:});
77 end
78 end
79 elseif isa(x,'mirdata')
80 if isframed(x)
81 warning('WARNING IN MIRFRAME: The input data is already decomposed into frames. No more frame decomposition.');
82 f = x;
83 else
84 x = purgedata(x);
85 dx = get(x,'Data');
86 if isa(x,'mirtemporal')
87 dt = get(x,'Time');
88 else
89 dt = get(x,'FramePos');
90 end
91 sf = get(x,'Sampling');
92 para = scanargin(varargin);
93 dx2 = cell(1,length(dx)); % magnitude in framed structure
94 dt2 = cell(1,length(dx)); % time positions in framed structure
95 fp = cell(1,length(dx)); % frame positions
96 for k = 1:length(dx) % For each audio file, ...
97 dxk = dx{k};
98 dtk = dt{k};
99 if strcmpi(para.wlength.unit,'s')
100 l = para.wlength.val*sf{k};
101 elseif strcmpi(para.wlength.unit,'sp')
102 l = para.wlength.val;
103 end
104 if strcmpi(para.hop.unit,'/1')
105 h = para.hop.val*l;
106 elseif strcmpi(para.hop.unit,'%')
107 h = para.hop.val*l*.01;
108 elseif strcmpi(para.hop.unit,'s')
109 h = para.hop.val*sf{k};
110 elseif strcmpi(para.hop.unit,'sp')
111 h = para.hop.val;
112 elseif strcmpi(para.hop.unit,'Hz')
113 h = sf{k}/para.hop.val;
114 end
115 l = floor(l);
116 dx2k = cell(1,length(dxk));
117 dt2k = cell(1,length(dxk));
118 fpk = cell(1,length(dxk));
119 for j = 1:length(dxk) % For each segment, ...
120 dxj = dxk{j};
121 dtj = dtk{j};
122 if not(isa(x,'mirtemporal'))
123 dxj = dxj';
124 dtj = dtj(1,:)';
125 end
126
127 n = floor((size(dxj,1)-l)/h)+1; % Number of frames
128 dx2j = zeros(l,n,size(dxj,3));
129 dt2j = zeros(l,n);
130 fpj = zeros(2,n);
131 if n < 1
132 disp('Frame length longer than total sequence size. No frame decomposition.');
133 dx2j = dxj(:,1,:);
134 dt2j = dtj;
135 fpj = [dtj(1) ; dtj(end)];
136 else
137 for i = 1:n % For each frame, ...
138 st = floor((i-1)*h+1);
139 stend = st+l-1;
140 dx2j(:,i,:) = dxj(st:stend,1,:);
141 dt2j(:,i) = dtj(st:stend);
142 fpj(:,i) = [dtj(st) dtj(stend)];
143 end
144 end
145 dx2k{j} = dx2j;
146 dt2k{j} = dt2j;
147 fpk{j} = fpj;
148 end
149 dx2{k} = dx2k;
150 dt2{k} = dt2k;
151 fp{k} = fpk;
152 end
153 if isa(x,'mirtemporal')
154 f = set(x,'Time',dt2,'Data',dx2,'FramePos',fp);
155 else
156 f = mirtemporal([],'Time',dt2,'Data',dx2,'FramePos',fp,...
157 'Sampling',get(x,'Sampling'),'Name',get(x,'Name'),...
158 'Label',get(x,'Label'),'Channels',get(x,'Channels'),...
159 'Centered',0,'Title',get(x,'Title'));
160 end
161 end
162 else
163 f = mirframe(miraudio(x),varargin{:});
164 end
165
166
167 function para = scanargin(v)
168 if not(isempty(v)) && isstruct(v{1})
169 if length(v) == 1
170 para = v{1};
171 else
172 para.wlength = v{1};
173 para.hop = v{2};
174 end
175 return
176 end
177 para.wlength.val = 0.05;
178 para.wlength.unit = 's';
179 para.hop.val = 0.5;
180 para.hop.unit = '/1';
181 nv = length(v);
182 i = 1;
183 j = 1;
184 while i <= nv
185 arg = v{i};
186 if strcmpi(arg,'WinLength') || strcmpi(arg,'Length')
187 if i < nv && isnumeric(v{i+1})
188 i = i+1;
189 j = 0;
190 para.wlength.val = v{i};
191 else
192 error('ERROR IN MIRFRAME: Incorrect use of Length option. See help mirframe.');
193 end
194 if i < nv && ischar(v{i+1}) && ...
195 (strcmpi(v{i+1},'s') || strcmpi(v{i+1},'sp'))
196 i = i+1;
197 para.wlength.unit = v{i};
198 end
199 elseif strcmpi(arg,'Hop')
200 if i < nv && isnumeric(v{i+1})
201 i = i+1;
202 j = 0;
203 para.hop.val = v{i};
204 else
205 error('ERROR IN MIRFRAME: Incorrect use of Hop option. See help mirframe.');
206 end
207 if i < nv && ischar(v{i+1}) && ...
208 (strcmpi(v{i+1},'%') || strcmpi(v{i+1},'/1') || ...
209 strcmpi(v{i+1},'s') || strcmpi(v{i+1},'sp') || ...
210 strcmpi(v{i+1},'Hz'))
211 i = i+1;
212 para.hop.unit = v{i};
213 end
214 elseif isnumeric(arg)
215 switch j
216 case 1
217 j = 2;
218 para.wlength.val = arg;
219 if i < nv && ischar(v{i+1}) && ...
220 (strcmpi(v{i+1},'s') || strcmpi(v{i+1},'sp'))
221 i = i+1;
222 para.wlength.unit = v{i};
223 end
224 case 2
225 j = 3;
226 para.hop.val = arg;
227 if i < nv && ischar(v{i+1}) && ...
228 (strcmpi(v{i+1},'%') || strcmpi(v{i+1},'/1') || ...
229 strcmpi(v{i+1},'s') || strcmpi(v{i+1},'sp') || ...
230 strcmpi(v{i+1},'Hz'))
231 i = i+1;
232 para.hop.unit = v{i};
233 end
234 otherwise
235 error('ERROR IN MIRFRAME: Syntax error. See help mirframe.');
236 end
237 elseif not(isempty(arg))
238 error('ERROR IN MIRFRAME: Syntax error. See help mirframe.');
239 end
240 i = i+1;
241 end