]> Creatis software - CreaPhase.git/blob - octave_packages/audio-1.1.4/au.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / audio-1.1.4 / au.m
1 ## Copyright (C) 2000 Paul Kienzle
2 ##
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
7 ##
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 ## GNU General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
15
16 ## y = au(x, fs, lo [, hi])
17 ##
18 ## Extract data from x for time range lo to hi in milliseconds.  If lo
19 ## is [], start at the beginning.  If hi is [], go to the end.  If hi is
20 ## not specified, return the single element at lo.  If lo<0, prepad the
21 ## signal to time lo.  If hi is beyond the end, postpad the signal to
22 ## time hi.
23
24 ## TODO: modify prepad and postpad so that they accept matrices.
25 function y=au(x,fs,lo,hi)
26   if nargin<3 || nargin>4,
27     usage("y = au(x, fs, lo [,hi])");
28   endif
29
30   if nargin<4, hi=lo; endif
31   if isempty(lo), 
32     lo=1; 
33   else
34     lo=fix(lo*fs/1000)+1;
35   endif
36   if isempty(hi),
37     hi=length(x);
38   else
39     hi=fix(hi*fs/1000)+1;
40   endif
41   if hi<lo, t=hi; hi=lo; lo=hi; endif
42   if (size(x,1)==1 || size(x,2)==1)
43     y=x(max(lo,1):min(hi,length(x)));
44     if (lo<1), y=prepad(y,length(y)-lo+1); endif
45     if (hi>length(x)), y=postpad(y,length(y)+hi-length(x)); endif
46   else
47     y=x(max(lo,1):min(hi,length(x)), :);
48     if (lo<1), y=[zeros(size(x,2),-lo+1) ; y]; endif
49     if (hi>length(x)), y=[y ; zeros(size(x,2),hi-length(x))]; endif
50   endif
51 endfunction