# HG changeset patch # User samer # Date 1421157797 0 # Node ID 3cedfd4549ef7892bcdce4b9f02cb8229d1a3077 # Parent 62e31e7980e6b28086a1509d5795ca18fa7de741 Code added since initial check in. diff -r 62e31e7980e6 -r 3cedfd4549ef arrows/dsp/aidct.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/arrows/dsp/aidct.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,9 @@ +% adct - arrow for idct +% +% aidct :: M:natural, N:natural -> arrow({[[M]]}, {[[N]]}, empty). +function o=aidct(M,N) + o=arr(@(x)idct(x,N)); + + % W=col(idct(eye(N)),M); +end + diff -r 62e31e7980e6 -r 3cedfd4549ef arrows/stats/accumstats1.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/arrows/stats/accumstats1.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,33 @@ +% accumstats1 - Arrow that collects 1st order statistics (struct version) +% +% accumstats1 :: arrow({[[N]]},{stats_struct(N)},stats_struct(N)). +% accumstats1 :: nonneg -> arrow({[[N]]},{stats_struct(N)},stats_struct(N)). +% +% If an argument is given, it is used as the effective memory length +% stats_struct(N) ::= struct { +% ag :: nonneg; +% amu :: [[N]]; +% }. + +function a=accumstats1(L) + if nargin<1, kappa=inf; else kappa=L*(L-1); end + + ss.ag=0; + ss.amu=0; + a=loop(ifx(isinf(kappa),@accum,@accum_forget),@(s)ss); + + function [y,ss]=accum_forget(x,ss), + n=size(x,2); + K=kappa/(kappa+n*ss.ag); + ss.ag=K*ss.ag+n; + ss.amu=K*ss.amu+sum(x,2); + y=ss; + end +end + +function [y,ss]=accum(x,ss), + ss.ag=ss.ag+size(x,2); + ss.amu=ss.amu+sum(x,2); + y=ss; +end + diff -r 62e31e7980e6 -r 3cedfd4549ef arrows/stats/astats_pca.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/arrows/stats/astats_pca.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,9 @@ +function o=astats_pca(I) + o=arr(@stats_pca1); + + function pca=stats_pca1(stats) + if isstruct(stats), pca=stats_pca(stats,I); + else pca.dummy=1; end + end +end + diff -r 62e31e7980e6 -r 3cedfd4549ef arrows/stats/astats_pca_cell.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/arrows/stats/astats_pca_cell.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,27 @@ +function o=astats_pca_cell(I) + o=arr(@stats_pca); + function pca=stats_pca(stats) + if iscell(stats) + m1=mstep(stats); + [V,D]=eig(m1.cov); + J=(1+size(D,1))-I; + pca.eigvecs=V(:,J); + pca.eigvals=diag(D); + pca.eigvals=pca.eigvals(J); + pca.mean=m1.mu; + else + pca.dummy=1; + end + end + function M=mstep(S) + M.mu=S{2}/S{1}; + M.cov=msym(S{3}/S{1} - M.mu*M.mu'); + end + + function M=mstep_z(S) + M.mu=zeros(size(S{3},1),1); + M.cov=S{3}/S{2}; + end + + function A=msym(X), A=(X+X')/2; end +end diff -r 62e31e7980e6 -r 3cedfd4549ef arrows/stats/stats_cov.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/arrows/stats/stats_cov.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,11 @@ +function C=stats_cov(stats) + m1=mstep(stats); + C=m1.cov; +end + +function M=mstep(S) + M.mu=S.amu/S.ag; + M.cov=msym(S.aco/S.ag - M.mu*M.mu'); +end + +function A=msym(X), A=(X+X')/2; end diff -r 62e31e7980e6 -r 3cedfd4549ef audio/aacfile.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/audio/aacfile.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,28 @@ +% aacfile - audio signal from AAC file using faad +% +% aacfile :: +% path ~'path to AAC encoded audio file', +% options { bits :: natural/16 ~'requested sample resolution' } +% -> signal(C,R). +% +% Options are also passed to RAWPIPE +function s=aacfile(file,varargin) + opts=options('bits',16,varargin{:}); + % this is just to get format information - it's no good for getting data + s1=sndpipe(sprintf(1,'faad -w %s',bash_arg(file))); + + % can't remember what's wrong with this... + % s=sndpipe(sprintf('faad -f 2 -w "%s" | sox -t raw -r %d -b 16 -c %d -e signed - -t au -',file,rate(s1),channels(s1)),'stringfn',@()sprintf('aacfile(''%s'')',file),varargin{:}); + switch opts.bits + case 16, bits_code=1; + case 24, bits_code=2; + case 32, bits_code=3; + otherwise, error('Illegal bits-per-sample'); + end + + s=rawpipe( sprintf('faad -f 2 -b %d -w %s',bits_code,bash_arg(file)), ... + audio_format(channels(s1),rate(s1),opts.bits), ... + 'stringfn',@()sprintf('aacfile(''%s'')',file), ... + opts); +end + diff -r 62e31e7980e6 -r 3cedfd4549ef audio/bash_arg.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/audio/bash_arg.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,7 @@ +function arg=bash_arg(locator) + arg=bash_esc(locator); + if strncmp(escaped,'http://',7) + arg = ['<(curl -L ',arg,')'] + end +end + diff -r 62e31e7980e6 -r 3cedfd4549ef audio/bash_esc.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/audio/bash_esc.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,26 @@ +% bash_esc - Convert string into escaped version for use in command lines +% bash_esc :: string -> string +function out=bash_esc(in,method) + import java.io.*; + if nargin<2, method=1; end + + cs=feature('DefaultCharacterSet'); + runtime=java.lang.Runtime.getRuntime(); + + switch method + case 1 % ALternative 1 + process=runtime.exec({'bash', '-c', 'printf "%q" "$(cat)"'}); + writer=OutputStreamWriter(process.getOutputStream(),cs); + writer.write(in); writer.close(); + + case 2 % ALternative 2 + process=runtime.exec('bash'); + writer=BufferedWriter(OutputStreamWriter(process.getOutputStream(),cs)); + writer.write('printf "%q" "$(cat)"'), writer.newLine(); + writer.write(in); writer.close(); + end + + rdr=BufferedReader(InputStreamReader(process.getInputStream(),cs)); + out=char(rdr.readLine()); + process.destroy(); +end diff -r 62e31e7980e6 -r 3cedfd4549ef audio/jsndfile.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/audio/jsndfile.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,30 @@ +% jsndfile - audio signal from audio file using JavaSound only +% jsndfile :: path, options(sndstream) -> signal(C,R). +% +% NB: Javazoom MP3 decoder does not get the length of the signal right. +function s=jsndfile(file,varargin) + string=sprintf('jsndfile(''%s'')',file); + if strncmp(file,'http://',7) + mkstream = @(q)urlstream(file,q); + else + mkstream = @(q)filestream(file,q); + end + s=sndstream(mkstream,'stringfn',@()string,varargin{:}); +end + +function [str,cleanup]=filestream(file,q) + jf=jfile(file); + if ~jf.exists(), error(sprintf('File %s does not exist',file)); end + if ~q, fprintf('Opening sound file: %s\n',file); end + str=austream(java.io.FileInputStream(jf)); + cleanup=@nop; +end + +function [str,cleanup]=urlstream(url,q) + jurl=java.net.URL(url); + % if ~jf.exists(), error(sprintf('File %s does not exist',file)); end + if ~q, fprintf('Opening URL: %s\n',url); end + str=austream(jurl.openStream()); + cleanup=@nop; +end + diff -r 62e31e7980e6 -r 3cedfd4549ef general/@struct/mpower.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/@struct/mpower.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,1 @@ +function x=mpower(s,f), x=s.(f); end diff -r 62e31e7980e6 -r 3cedfd4549ef general/@struct/mrdivide.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/@struct/mrdivide.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,3 @@ +function y=mrdivide(x,f) + if isstruct(f), f=fields(f); end + y=rmfield(x,f); diff -r 62e31e7980e6 -r 3cedfd4549ef general/@struct/or.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/@struct/or.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,3 @@ +function B=or(B,A) + for f=fields(A)', [B.(f{1})]=A.(f{1}); end + diff -r 62e31e7980e6 -r 3cedfd4549ef general/arrutils/allequal.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/arrutils/allequal.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,4 @@ +% allequal - Test if all elements of vector are equal +% allequal :: [[N]->A] -> bool. +function f=allequal(x), f=all(x==x(1)); + diff -r 62e31e7980e6 -r 3cedfd4549ef general/arrutils/mapels.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/arrutils/mapels.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,13 @@ +function Z=mapels(f,X) +% mapels - Map a function of a scalar over one or more arrays +% +% mapels :: +% (A->B), +% [Size->A] +% -> [Size->B]. + +z1=f(X(1)); +Z=repmat(z1,size(X)); +for i=2:numel(X) + Z(i)=f(X(i)); +end diff -r 62e31e7980e6 -r 3cedfd4549ef general/arrutils/orderrows.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/arrutils/orderrows.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,6 @@ +function I=orderrows(X) +% orderrows - return sort order +% +% orderrows :: [[N,M]] -> [[N]->[N]]. + +[Y,I]=sortrows(X); diff -r 62e31e7980e6 -r 3cedfd4549ef general/arrutils/zipels.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/arrutils/zipels.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,16 @@ +function Z=zipels(f,varargin) +% zipels - Zip a scalar function of several arguments over several arrays +% +% zipels :: +% (A{:}->B) +% [Size->A{1}], +% [Size->A{2}], +% .. +% -> [Size->B]. + +fN=(f<=length(varargin)); % fN takes a tuple instead of multiple args +nth=@(i)@(x)x(i); +Z=repmat(fN(cellmap(nth(1),varargin)),size(varargin{1})); +for i=2:numel(varargin{1}) + Z(i)=fN(cellmap(nth(i),varargin)); +end diff -r 62e31e7980e6 -r 3cedfd4549ef general/cellutils/box.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/cellutils/box.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,10 @@ +% box - wrap value in cell +% box :: A -> cell {A}. +% +% NB. this function hides Matlab's box function which +% controls the appearance of the current axis. HOWEVER, +% if this function is called with no return value, then +% box('on') and box('off') control the axis box as before. +% None of the other box arguments patterns work. [sorry] +function y=box(x), + if nargout==1, y={x}; else set(gca,'Box',x); end diff -r 62e31e7980e6 -r 3cedfd4549ef general/cellutils/cfold.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/cellutils/cfold.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,10 @@ +function X=cfold(fn,x) +% fold - Fold combinator for cells +% +% fold :: (X,X->X), cells(A) -> X. +% +% This function applies an associative operator to a list of arguments, +% as if the list was written out with the operator between consecutive +% elements, eg fold(@plus,{1,2,3,4}) = 1+2+3+4. + +if isempty(x), X={}; else X=cfoldl(fn,head(x),next(x)); end diff -r 62e31e7980e6 -r 3cedfd4549ef general/cellutils/fst.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/cellutils/fst.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,3 @@ +% fst - first element of pair +% fst :: pair(A,B) -> A. +function x=fst(c), x=c{1}; end diff -r 62e31e7980e6 -r 3cedfd4549ef general/cellutils/snd.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/cellutils/snd.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,3 @@ +% snd - first element of pair +% snd :: pair(A,B) -> B. +function x=snd(c), x=c{2}; end diff -r 62e31e7980e6 -r 3cedfd4549ef general/cellutils/unbox.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/cellutils/unbox.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,3 @@ +% unbox - unwrap single cell +% unbox :: cell {A} -> A. +function y=unbox(x), y=x{1}; diff -r 62e31e7980e6 -r 3cedfd4549ef general/eval_fields.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/eval_fields.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,14 @@ +% eval_fields - Apply function to all member fields of structure +% +% eval_fields :: +% struct(fields(N,A{:}->B)), +% A{:}, +% -> struct(fields(N,B)). +% +% fields(N,A) denotes a list of struct field type declarates with +% names from list N and types all equal to A, eg +% fields({'a','b','c'},real) == {a::real, b::real, c::real}. + +function x=eval_fields(y,varargin) + args=varargin; + x=map_fields(@(f)feval(f,args{:}),y); diff -r 62e31e7980e6 -r 3cedfd4549ef general/funutils/@function_handle/colon.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/funutils/@function_handle/colon.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,6 @@ +% colon - unary function application +% +% colon :: (A->B{:}), A -> B{:}. +function varargout=colon(f,x) + [varargout{1:nargout}]=f(x); +end diff -r 62e31e7980e6 -r 3cedfd4549ef general/funutils/@function_handle/ldivide.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/funutils/@function_handle/ldivide.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,6 @@ +% ldivide - Bind arguments to a function using Matlab closure +% +% ldivide :: cell A(1:M), (A{1:N}=>B{:}) -> (A{M+1:N}=>B{:}). + +function g=ldivide(args,f), g=rdivide(f,args); + diff -r 62e31e7980e6 -r 3cedfd4549ef general/funutils/@function_handle/le.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/funutils/@function_handle/le.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,20 @@ +% le - distribute cell argument to multiple in arguments +% +% le :: (A{1:N}=>B{:}), M:natural -> (cell {A{1:M}}, A{M+1:N} -> B{:}). +% +% f<=5 is a function which takes any number of arguments but where the first +% argument is assumed to be a cell array of M elements which are distributed +% to the first M arguments of f. + +function g=le(f,I) + if nargout(f)==1, g=@q1; else g=@qn; end + + function x=q1(varargin) + args1=varargin{1}; + x=f(args1{1:I},varargin{2:end}); + end + function varargout=qn(varargin) + args1=varargin{1}; + [varargout{1:nargout}]=f(args1{1:I},varargin{2:end}); + end +end diff -r 62e31e7980e6 -r 3cedfd4549ef general/funutils/@function_handle/mldivide.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/funutils/@function_handle/mldivide.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,8 @@ +% mldivide - Bind one argument to a function using Matlab closure +% +% mrdivide :: +% A{1} ~'first argument', +% (A{1:N}->B{1:L}) ~'func from N inputs to L outputs' +% -> (A{2:N}->B{1:L}) ~'func from remaining arguments to returns'. + +function g=mldivide(x,f), g=mrdivide(f,x); diff -r 62e31e7980e6 -r 3cedfd4549ef general/funutils/@function_handle/with_field.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/funutils/@function_handle/with_field.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,9 @@ +% with_field - Apply operator to structure +% with_field :: +% N:string ~'field name', +% (A->B) ~'function' +% -> (struct { N : A } -> struct { N : B}). +function g=with_field(nm,f) + g=@op; + function x=op(x), x.(nm)=f(x.(nm)); end +end diff -r 62e31e7980e6 -r 3cedfd4549ef general/map_fields.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/map_fields.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,13 @@ +% map_fields - Apply function to all member fields of structure +% +% map_fields :: +% (A->B) ~'function from As to Bs', +% struct(fields(N,A)) +% -> struct(fields(N,B)). +% +% fields(N,A) denotes a list of struct field type declarates with +% names from list N and types all equal to A, eg +% fields({'a','b','c'},real) == {a::real, b::real, c::real}. + +function y=map_fields(fn,x) + y=foldl(@(s,f)setfield(s,f,fn(s.(f))),x,fields(x)); diff -r 62e31e7980e6 -r 3cedfd4549ef general/rec.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/rec.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,5 @@ +% rec - elementary struct constructor +% +% rec :: N:string, A -> struct { N::A }. +function s=rec(fn,val) + s.(fn)=val; diff -r 62e31e7980e6 -r 3cedfd4549ef general/with_columns.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/with_columns.m Tue Jan 13 14:03:17 2015 +0000 @@ -0,0 +1,9 @@ +function varargout=with_columns(f,x,varargin) + slices=cslices(x,2); + [varargout{1:nargout}]=f(slices{:},varargin{:}); +end + +function y=cslices(x,dim) + if nargin<2, dim=length(size1(x)); end + y=map(@(i)sli(x,i,dim),num2cell(1:size(x,dim))); +end