samer@1
|
1 % signal - Base class for signal
|
samer@1
|
2 %
|
samer@1
|
3 % signal :: signal(C:natural,R:nonneg).
|
samer@1
|
4 %
|
samer@1
|
5 % The signal(C,R) type denotes the type of a signal with C
|
samer@1
|
6 % channels and a sampling rate of R.
|
samer@1
|
7 %
|
samer@1
|
8 % The base signal class cannot be used without subclassing since
|
samer@1
|
9 % any attempt to instantiate the live signal generator will throw
|
samer@1
|
10 % an exception.
|
samer@1
|
11 %
|
samer@1
|
12 % METHODS
|
samer@1
|
13 % channels :: signal(C,R) -> natural.
|
samer@1
|
14 % rate :: signal(C,R) -> nonneg.
|
samer@1
|
15 % construct:: signal(C,R) -> livesig(C).
|
samer@1
|
16 % length :: signal(C,R) -> natural.
|
samer@1
|
17 % gather :: signal(C,R), options -> [[C,N]].
|
samer@1
|
18 % gathern :: N:natural, signal(C,R), options -> [[C,N]], natural.
|
samer@1
|
19 %
|
samer@1
|
20 % livesig(C) :== struct {
|
samer@1
|
21 % start :: void->void;
|
samer@1
|
22 % stop :: void->void;
|
samer@1
|
23 % dispose :: void->void;
|
samer@1
|
24 % reader :: N:natural -> (void->[[C,N]]);
|
samer@1
|
25 % }
|
samer@1
|
26
|
samer@1
|
27 classdef signal
|
samer@1
|
28 properties (GetAccess=private, SetAccess=immutable)
|
samer@1
|
29 end
|
samer@1
|
30 methods
|
samer@1
|
31 function s=signal, end
|
samer@1
|
32
|
samer@1
|
33 function s=and(s1,s2), s=sigcat(s1,s2); end
|
samer@1
|
34 function y=cache(x), y=reclock(rate(x),sigarray(gather(x))); end
|
samer@1
|
35 function c=channels(s), error('number of channels undefined'); end
|
samer@1
|
36 function s=construct(sig), error('Cannot construct base signal class'); end
|
samer@1
|
37 function display(a)
|
samer@1
|
38 disp(sprintf(' %s :: signal(%s,%s)',tostring(a),fmt(channels(a)),fmt(rate(a))));
|
samer@1
|
39 function s=fmt(x), if isnan(x), s='_'; else s=num2str(x); end; end
|
samer@1
|
40 end
|
samer@1
|
41
|
samer@1
|
42 function y=map(f,x), y=sigmap(f,x); end
|
samer@1
|
43 function s=mpower(a,b), s=resample(b,a); end
|
samer@1
|
44 function s=or(s1,s2), s=sigbinop(@vertcat,s1,s2,@plus); end
|
samer@1
|
45 function r=rate(s), error('sampling rate undefined'); end
|
samer@1
|
46 function s2=reclock(r,s1), s2=sigreclock(r,s1); end
|
samer@1
|
47 function y=drop(n,x), y=sigdrop(n,x); end
|
samer@1
|
48 function y=take(n,x), y=sigtake(n,x); end
|
samer@1
|
49
|
samer@1
|
50 function y=dropt(t,x),
|
samer@1
|
51 if isnan(rate(x)), error('Cannot dropt without definite sampling rate'); end
|
samer@1
|
52 y=sigdrop(round(t*rate(x)),x);
|
samer@1
|
53 end
|
samer@1
|
54
|
samer@1
|
55 function y=taket(t,x),
|
samer@1
|
56 if isnan(rate(x)), error('Cannot taket without definite sampling rate'); end
|
samer@1
|
57 y=sigtake(round(t*rate(x)),x);
|
samer@1
|
58 end
|
samer@1
|
59
|
samer@1
|
60 function y=cycle(x),
|
samer@1
|
61 y=siglzcat(x,@cyclef);
|
samer@1
|
62 function [s1,sx]=cyclef, s1=x; sx=@cyclef; end
|
samer@1
|
63 end
|
samer@1
|
64
|
samer@1
|
65 function s2=resample(f2,s1,varargin)
|
samer@1
|
66 if isnan(rate(s1)), error('no sample rate set'); end
|
samer@1
|
67 if rate(s1)==f2, s2=s1;
|
samer@1
|
68 else, s2=sigresample(f2,s1,varargin{:}); end
|
samer@1
|
69 end
|
samer@1
|
70 end
|
samer@1
|
71 end
|