Home > chronux_2_00 > spikesort > utility > datatools > sindata.m

sindata

PURPOSE ^

SINDATA Generate noisy sinuisoidal data.

SYNOPSIS ^

function [Y,t] = sindata(dur, Fs, W, A, P, N)

DESCRIPTION ^

SINDATA           Generate noisy sinuisoidal data.
   [Y,t] = SINDATA(DUR,Fs) returns vectors t and Y such that plot(t,Y)
   draws the sinusoid sin(2*pi*t) on the interval 0..DUR with sampling
   rate Fs.

   [Y,t] = SINDATA(DUR,Fs,W), for W scalar, instead returns Y as the
   sinusoid sin(2*pi*W*t).  If W is a vector, t is unchanged, but Y will
   be a matrix with j'th row given by the sinusoid sin(2*pi*W(j)*t).

   [Y,t] = SINDATA(DUR,Fs,W,A,P,N) further specifies amplitude, phase,
   and noise parameters.  Here, when arguments W, A, P or N all have the
   the same number of elements M, Y will then have M rows with j'th row
   of form 
                 A(j) * sin(2*pi*W(j)*t + P(j)) + N(j)*WGN
   where WGN is white gaussian noise.  When any of W, A, P or N is of
   length M > 1, the other arguments can be the empty matrix, scalar or a
   vectors of length M.  If the empty matrix is given for an argument,
   the default value is assumed (W = 1, A = 1, P = 0, N = 0).  If any
   argument is scalar when another argument is of length M, the scalar
   argument is treated as a repeated value.  E.g., the following are
   equivalent:
                  SINDATA(10,100,[1 2], 3, [pi/2 0], [])
                  SINDATA(10,100,[1 2], [3 3], [pi/2 0], [0 0])

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [Y,t] = sindata(dur, Fs, W, A, P, N)
0002 %SINDATA           Generate noisy sinuisoidal data.
0003 %   [Y,t] = SINDATA(DUR,Fs) returns vectors t and Y such that plot(t,Y)
0004 %   draws the sinusoid sin(2*pi*t) on the interval 0..DUR with sampling
0005 %   rate Fs.
0006 %
0007 %   [Y,t] = SINDATA(DUR,Fs,W), for W scalar, instead returns Y as the
0008 %   sinusoid sin(2*pi*W*t).  If W is a vector, t is unchanged, but Y will
0009 %   be a matrix with j'th row given by the sinusoid sin(2*pi*W(j)*t).
0010 %
0011 %   [Y,t] = SINDATA(DUR,Fs,W,A,P,N) further specifies amplitude, phase,
0012 %   and noise parameters.  Here, when arguments W, A, P or N all have the
0013 %   the same number of elements M, Y will then have M rows with j'th row
0014 %   of form
0015 %                 A(j) * sin(2*pi*W(j)*t + P(j)) + N(j)*WGN
0016 %   where WGN is white gaussian noise.  When any of W, A, P or N is of
0017 %   length M > 1, the other arguments can be the empty matrix, scalar or a
0018 %   vectors of length M.  If the empty matrix is given for an argument,
0019 %   the default value is assumed (W = 1, A = 1, P = 0, N = 0).  If any
0020 %   argument is scalar when another argument is of length M, the scalar
0021 %   argument is treated as a repeated value.  E.g., the following are
0022 %   equivalent:
0023 %                  SINDATA(10,100,[1 2], 3, [pi/2 0], [])
0024 %                  SINDATA(10,100,[1 2], [3 3], [pi/2 0], [0 0])
0025 
0026 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Defaults %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0027 freq = 1;      amp = 1;      phase = 0;      noiseamp = 0;
0028 
0029 %%%%%%%%%%%%%%%%%%%%%%%%%% Argument Checking %%%%%%%%%%%%%%%%%%%%%%%%%
0030 % first, fill in missing parameters
0031 if ((nargin < 6) || isempty(N)),  N = noiseamp; end;
0032 if ((nargin < 5) || isempty(P)),  P = phase;    end;
0033 if ((nargin < 4) || isempty(A)),  A = amp;      end;
0034 if ((nargin < 3) || isempty(W)),  W = freq;     end;
0035 
0036 % next, confirm sizes are consistent
0037 W = col(W);  A = col(A);  P = col(P);  N = col(N);
0038 sz = [numel(W)  numel(A)  numel(P)  numel(N)];
0039 M = unique(sz);  if (any(M==1)), M(M==1) = [];  end;  % (ignore scalars for now)
0040 if ((length(M) > 1) || (any(sz==0)))  % any empties at this pt were caused by col
0041     error('Sinusoidal parameters must all be vectors with the same number of elements.'); 
0042 end
0043 if (isempty(M)),  M = 1;  end;
0044 
0045 % finally, expand scalars
0046 expand = ones(M,1);
0047 if (M > 1),
0048     if (numel(W)==1),  W = W(expand);  end;
0049     if (numel(A)==1),  A = A(expand);  end;
0050     if (numel(P)==1),  P = P(expand);  end;
0051     if (numel(N)==1),  N = N(expand);  end;
0052 end
0053 
0054 %%%%%%%%%%%%%%%%%%%%%%%%%%%%% Make the data %%%%%%%%%%%%%%%%%%%%%%%%%%
0055 t = (0:1/Fs:dur);    Nt = length(t);
0056 Y = diag(A) * sin(2*pi*diag(W)*repmat(t,M,1) + repmat(P,1,Nt)) + diag(N)*randn(M,Nt);
0057 
0058 
0059 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0060 %%%%%%%%%%%%%%%%%%%%%%%%%% Helper Functions %%%%%%%%%%%%%%%%%%%%%%%%%%
0061 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0062 function v = col(v)  % force col vector; or empty if not a scalar or 1-Dvector.
0063 s = isvectord(v);
0064 if (s == 2), v = v';
0065 elseif ((s == 0) && (numel(v) > 1)), v = [];
0066 end

Generated on Fri 15-Aug-2008 11:35:42 by m2html © 2003