changeset 44:3cedfd4549ef

Code added since initial check in.
author samer
date Tue, 13 Jan 2015 14:03:17 +0000
parents 62e31e7980e6
children 1ff748470e1d
files arrows/dsp/aidct.m arrows/stats/accumstats1.m arrows/stats/astats_pca.m arrows/stats/astats_pca_cell.m arrows/stats/stats_cov.m audio/aacfile.m audio/bash_arg.m audio/bash_esc.m audio/jsndfile.m general/@struct/mpower.m general/@struct/mrdivide.m general/@struct/or.m general/arrutils/allequal.m general/arrutils/mapels.m general/arrutils/orderrows.m general/arrutils/zipels.m general/cellutils/box.m general/cellutils/cfold.m general/cellutils/fst.m general/cellutils/snd.m general/cellutils/unbox.m general/eval_fields.m general/funutils/@function_handle/colon.m general/funutils/@function_handle/ldivide.m general/funutils/@function_handle/le.m general/funutils/@function_handle/mldivide.m general/funutils/@function_handle/with_field.m general/map_fields.m general/rec.m general/with_columns.m
diffstat 30 files changed, 345 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /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
+
--- /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
+
--- /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
+
--- /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
--- /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
--- /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
+
--- /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
+
--- /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
--- /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
+
--- /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
--- /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);
--- /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
+	
--- /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));
+
--- /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
--- /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);
--- /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
--- /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
--- /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
--- /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
--- /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
--- /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}; 
--- /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);
--- /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
--- /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);
+
--- /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
--- /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);
--- /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
--- /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));
--- /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; 
--- /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