2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2011 - DIGITEO - Antoine ELIAS
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
12 /*--------------------------------------------------------------------------*/
14 #include "signal_gw.hxx"
17 #include "function.hxx"
21 #include "localization.h"
26 extern void C2F(tscccf)(double *x, double *y, int *length, double *cxy, double *xymean, int *lag, int *error);
30 /*--------------------------------------------------------------------------*/
31 types::Function::ReturnValue sci_corr(types::typed_list &in, int _iRetCount, types::typed_list &out)
33 //check input parameters
34 if(in.size() < 2 || in.size() > 5)
36 ScierrorW(77, _W("%ls: Wrong number of input argument(s): %d to %d expected.\n"), L"corr", 2, 5);
37 return types::Function::Error;
43 sciprintW(_W("%ls: Need to plug external call"), L"corr");
44 return types::Function::Error;
46 types::String* pS = in[0]->getAs<types::String>();
47 if(pS->getSize() == 1 && pS->get(0)[0] == L'f')
48 {//[cov,mean]=corr('fft',xmacro,[ymacro],n,sect)
54 types::Callable* pXFunction = NULL;
55 types::Callable* pYFunction = NULL;
57 //check input parameters
58 if(in.size() < 4 || in.size() > 5)
60 ScierrorW(77, _W("%ls: Wrong number of input argument(s): %d to %d expected.\n"), L"corr", 4, 5);
61 return types::Function::Error;
65 int iPos = (int)(in.size() - 1);
66 if(in[iPos]->isDouble() == false || in[iPos]->getAs<types::Double>()->isScalar() == false)
68 ScierrorW(999, _W("%ls: Wrong type for input argument #%d: A scalar expected.\n"), L"corr", iPos + 1);
69 return types::Function::Error;
72 iSect = (int)in[iPos]->getAs<types::Double>()->get(0);
76 if(in[iPos]->isDouble() == false || in[iPos]->getAs<types::Double>()->isScalar() == false)
78 ScierrorW(999, _W("%ls: Wrong type for input argument #%d: A scalar expected.\n"), L"corr", iPos + 1);
79 return types::Function::Error;
82 iTotalSize = (int)in[iPos]->getAs<types::Double>()->get(0);
85 if(in[1]->isCallable() == false)
87 ScierrorW(999, _W("%ls: Wrong type for input argument #%d: A function expected.\n"), L"corr", 2);
88 return types::Function::Error;
91 pXFunction = in[1]->getAs<types::Callable>();
97 if(in[2]->isCallable() == false)
99 ScierrorW(999, _W("%ls: Wrong type for input argument #%d: A function expected.\n"), L"corr", 3);
100 return types::Function::Error;
103 pYFunction = in[2]->getAs<types::Callable>();
108 else if(pS->getSize() == 1 && pS->get(0)[0] == L'u')
113 ScierrorW(999, _W("%ls: Wrong value for input argument #%d: Must be in the set {%ls}.\n"), L"corr", 1, L"'fft', 'update'");
114 return types::Function::Error;
118 {//usual case [cov,mean]=corr(x,[y],nlags)
120 int iCorrelation = 0;
121 types::Double* pDblX = NULL;
122 types::Double* pDblY = NULL;
123 types::Double* pDblCorrelation = NULL;
124 types::Double* pDblMean = NULL;
129 //check input parameters
130 if(in.size() < 2 || in.size() > 3)
132 ScierrorW(77, _W("%ls: Wrong number of input argument(s): %d to %d expected.\n"), L"corr", 2, 3);
133 return types::Function::Error;
136 //get last parameter nlags
137 int iPos = (int)(in.size() - 1);
138 if(in[iPos]->isDouble() == false || in[iPos]->getAs<types::Double>()->isScalar() == false)
140 ScierrorW(999, _W("%ls: Wrong type for input argument #%d: A scalar expected.\n"), L"corr", iPos + 1);
141 return types::Function::Error;
144 iCorrelation = (int)in[iPos]->getAs<types::Double>()->get(0);
145 pDblCorrelation = new types::Double(1, iCorrelation);
149 if(in[1]->isDouble() == false)
151 ScierrorW(999, _W("%ls: Wrong type for input argument #%d: Matrix expected.\n"), L"corr" ,2);
152 return types::Function::Error;
155 pDblY = in[1]->getAs<types::Double>();
157 if(in[0]->isDouble() == false)
159 ScierrorW(999, _W("%ls: Wrong type for input argument #%d: Matrix expected.\n"), L"corr" ,1);
160 return types::Function::Error;
163 pDblX = in[0]->getAs<types::Double>();
165 if(pDblX->getSize() != pDblY->getSize())
167 ScierrorW(60, _W("%ls: Wrong size for argument: Incompatible dimensions.\n"), L"corr");
168 return types::Function::Error;
173 if(in[0]->isDouble() == false)
175 ScierrorW(999, _W("%ls: Wrong type for input argument #%d: Matrix expected.\n"), L"corr" ,1);
176 return types::Function::Error;
179 pDblX = in[0]->getAs<types::Double>();
183 iSize = pDblX->getSize();
185 C2F(tscccf)(pDblX->get(), pDblY->get(), &iSize, pDblCorrelation->get(), pdblMean, &iCorrelation, &iErr);
188 ScierrorW(999, _W("%ls: Too many coefficients are required.\n"), L"corr");
189 return types::Function::Error;
192 out.push_back(pDblCorrelation);
198 pDblMean = new types::Double(1, 2);
202 pDblMean = new types::Double(1, 1);
205 pDblMean->set(pdblMean);
206 out.push_back(pDblMean);
209 return types::Function::OK;