Output, input, function

4 次查看(过去 30 天)
Hi everyone,
I have a problem with Inputting one matrix to other. I have one script where i create a array, and i would like to input the matrix "mixedsig" to other scrpit. When i run the second program my code is stopping in first if and i get "you must supply the mixed data as input argument".
Do you have any idea how to fixed it?
Thank you in advance.
function [sig,mixedsig]=demosig()
N=500; %data size
v=[0:N-1];
sig=[];
sig(1,:)=sin(v/2); %sinusoid
sig(2,:)=((rem(v,23)-11)/9).^5; %funny curve
sig(3,:)=((rem(v,27)-13)/9); %saw-tooth
sig(4,:)=((rand(1,N)<.5)*2-1).*log(rand(1,N)); %impulsive noise
for t=1:4
sig(t,:)=sig(t,:)/std(sig(t,:));
end
%remove mean (not really necessary)
[sig mean]=remmean(sig);
%create mixtures
Aorig=rand(size(sig,1));
mixedsig=(Aorig*sig);
end
dfdfd
function [Out1, Out2, Out3] = fastica(mixedsig, varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Check some basic requirements of the data
if nargin == 0,
error ('You must supply the mixed data as input argument.');
end
if length (size (mixedsig)) > 2,
error ('Input data can not have more than two dimensions.');
end
if any (any (isnan (mixedsig))),
error ('Input data contains NaN''s.');
end
if ~isa (mixedsig, 'double')
fprintf ('Warning: converting input data into regular (double) precision.\n');
mixedsig = double (mixedsig);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Remove the mean and check the data
[mixedsig, mixedmean] = remmean(mixedsig);
[Dim, NumOfSampl] = size(mixedsig);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Default values for optional parameters
% All
verbose = 'on';
% Default values for 'pcamat' parameters
firstEig = 1;
lastEig = Dim;
interactivePCA = 'off';
% Default values for 'fpica' parameters
approach = 'defl';
numOfIC = Dim;
g = 'pow3';
finetune = 'off';
a1 = 1;
a2 = 1;
myy = 1;
stabilization = 'off';
epsilon = 0.0001;
maxNumIterations = 1000;
maxFinetune = 5;
initState = 'rand';
guess = 0;
sampleSize = 1;
displayMode = 'off';
displayInterval = 1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Parameters for fastICA - i.e. this file
b_verbose = 1;
jumpPCA = 0;
jumpWhitening = 0;
only = 3;
userNumOfIC = 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Read the optional parameters
if (rem(length(varargin),2)==1)
error('Optional parameters should always go by pairs');
else
for i=1:2:(length(varargin)-1)
if ~ischar (varargin{i}),
error (['Unknown type of optional parameter name (parameter' ...
' names must be strings).']);
end
% change the value of parameter
switch lower (varargin{i})
case 'stabilization'
stabilization = lower (varargin{i+1});
case 'maxfinetune'
maxFinetune = varargin{i+1};
case 'samplesize'
sampleSize = varargin{i+1};
case 'verbose'
verbose = lower (varargin{i+1});
% silence this program also
if strcmp (verbose, 'off'), b_verbose = 0; end
case 'firsteig'
firstEig = varargin{i+1};
case 'lasteig'
lastEig = varargin{i+1};
case 'interactivepca'
interactivePCA = lower (varargin{i+1});
case 'approach'
approach = lower (varargin{i+1});
case 'numofic'
numOfIC = varargin{i+1};
% User has supplied new value for numOfIC.
% We'll use this information later on...
userNumOfIC = 1;
case 'g'
g = lower (varargin{i+1});
case 'finetune'
finetune = lower (varargin{i+1});
case 'a1'
a1 = varargin{i+1};
case 'a2'
a2 = varargin{i+1};
case {'mu', 'myy'}
myy = varargin{i+1};
case 'epsilon'
epsilon = varargin{i+1};
case 'maxnumiterations'
maxNumIterations = varargin{i+1};
case 'initguess'
% no use setting 'guess' if the 'initState' is not set
initState = 'guess';
guess = varargin{i+1};
case 'displaymode'
displayMode = lower (varargin{i+1});
case 'displayinterval'
displayInterval = varargin{i+1};
case 'pcae'
% calculate if there are enought parameters to skip PCA
jumpPCA = jumpPCA + 1;
E = varargin{i+1};
case 'pcad'
% calculate if there are enought parameters to skip PCA
jumpPCA = jumpPCA + 1;
D = varargin{i+1};
case 'whitesig'
% calculate if there are enought parameters to skip PCA and whitening
jumpWhitening = jumpWhitening + 1;
whitesig = varargin{i+1};
case 'whitemat'
% calculate if there are enought parameters to skip PCA and whitening
jumpWhitening = jumpWhitening + 1;
whiteningMatrix = varargin{i+1};
case 'dewhitemat'
% calculate if there are enought parameters to skip PCA and whitening
jumpWhitening = jumpWhitening + 1;
dewhiteningMatrix = varargin{i+1};
case 'only'
% if the user only wants to calculate PCA or...
switch lower (varargin{i+1})
case 'pca'
only = 1;
case 'white'
only = 2;
case 'all'
only = 3;
end
otherwise
% Hmmm, something wrong with the parameter string
error(['Unrecognized parameter: ''' varargin{i} '''']);
end;
end;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% print information about data
if b_verbose
fprintf('Number of signals: %d\n', Dim);
fprintf('Number of samples: %d\n', NumOfSampl);
end
% Check if the data has been entered the wrong way,
% but warn only... it may be on purpose
if Dim > NumOfSampl
if b_verbose
fprintf('Warning: ');
fprintf('The signal matrix may be oriented in the wrong way.\n');
fprintf('In that case transpose the matrix.\n\n');
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculating PCA
% We need the results of PCA for whitening, but if we don't
% need to do whitening... then we dont need PCA...
if jumpWhitening == 3
if b_verbose,
fprintf ('Whitened signal and corresponding matrises supplied.\n');
fprintf ('PCA calculations not needed.\n');
end;
else
% OK, so first we need to calculate PCA
% Check to see if we already have the PCA data
if jumpPCA == 2,
if b_verbose,
fprintf ('Values for PCA calculations supplied.\n');
fprintf ('PCA calculations not needed.\n');
end;
else
% display notice if the user entered one, but not both, of E and D.
if (jumpPCA > 0) & (b_verbose),
fprintf ('You must suply all of these in order to jump PCA:\n');
fprintf ('''pcaE'', ''pcaD''.\n');
end;
% Calculate PCA
[E, D]=pcamat(mixedsig, firstEig, lastEig, interactivePCA, verbose);
end
end
% skip the rest if user only wanted PCA
if only > 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Whitening the data
% Check to see if the whitening is needed...
if jumpWhitening == 3,
if b_verbose,
fprintf ('Whitening not needed.\n');
end;
else
% Whitening is needed
% display notice if the user entered some of the whitening info, but not all.
if (jumpWhitening > 0) & (b_verbose),
fprintf ('You must suply all of these in order to jump whitening:\n');
fprintf ('''whiteSig'', ''whiteMat'', ''dewhiteMat''.\n');
end;
% Calculate the whitening
[whitesig, whiteningMatrix, dewhiteningMatrix] = whitenv ...
(mixedsig, E, D, verbose);
end
end % if only > 1
% skip the rest if user only wanted PCA and whitening
if only > 2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculating the ICA
% Check some parameters
% The dimension of the data may have been reduced during PCA calculations.
% The original dimension is calculated from the data by default, and the
% number of IC is by default set to equal that dimension.
Dim = size(whitesig, 1);
% The number of IC's must be less or equal to the dimension of data
if numOfIC > Dim
numOfIC = Dim;
% Show warning only if verbose = 'on' and user supplied a value for 'numOfIC'
if (b_verbose & userNumOfIC)
fprintf('Warning: estimating only %d independent components\n', numOfIC);
fprintf('(Can''t estimate more independent components than dimension of data)\n');
end
end
% Calculate the ICA with fixed point algorithm.
[A, W] = fpica (whitesig, whiteningMatrix, dewhiteningMatrix, approach, ...
numOfIC, g, finetune, a1, a2, myy, stabilization, epsilon, ...
maxNumIterations, maxFinetune, initState, guess, sampleSize, ...
displayMode, displayInterval, verbose);
% Check for valid return
if ~isempty(W)
% Add the mean back in.
if b_verbose
fprintf('Adding the mean back to the data.\n');
end
icasig = W * mixedsig + (W * mixedmean) * ones(1, NumOfSampl);
%icasig = W * mixedsig;
if b_verbose & ...
(max(abs(W * mixedmean)) > 1e-9) & ...
(strcmp(displayMode,'signals') | strcmp(displayMode,'on'))
fprintf('Note that the plots don''t have the mean added.\n');
end
else
icasig = [];
end
end % if only > 2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The output depends on the number of output parameters
% and the 'only' parameter.
if only == 1 % only PCA
Out1 = E;
Out2 = D;
elseif only == 2 % only PCA & whitening
if nargout == 2
Out1 = whiteningMatrix;
Out2 = dewhiteningMatrix;
else
Out1 = whitesig;
Out2 = whiteningMatrix;
Out3 = dewhiteningMatrix;
end
else % ICA
if nargout == 2
Out1 = A;
Out2 = W;
else
Out1 = icasig;
Out2 = A;
Out3 = W;
end
end

采纳的回答

Voss
Voss 2022-11-22
You have to pass mixedsig to the fastica function as an input argument, e.g., from within demosig:
function [sig,mixedsig]=demosig()
N=500; %data size
v=[0:N-1];
sig=[];
sig(1,:)=sin(v/2); %sinusoid
sig(2,:)=((rem(v,23)-11)/9).^5; %funny curve
sig(3,:)=((rem(v,27)-13)/9); %saw-tooth
sig(4,:)=((rand(1,N)<.5)*2-1).*log(rand(1,N)); %impulsive noise
for t=1:4
sig(t,:)=sig(t,:)/std(sig(t,:));
end
%remove mean (not really necessary)
[sig mean]=remmean(sig);
%create mixtures
Aorig=rand(size(sig,1));
mixedsig=(Aorig*sig);
% call fastica function with mixedsig as input:
fastica(mixedsig)
end
Or after demosig, from somewhere else (i.e., another script or function or the command line):
[sig,mixedsig] = demosig() % demosig as you originally had it, where it doesn't call fastica
fastica(mixedsig) % call fastica here
  6 个评论
Walter Roberson
Walter Roberson 2022-11-24
[sig,mixedsig]=demosig()
fastica(mixedsig)
Note that demosig already calls fastica passing in mixedsig

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by