2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2011 - DIGITEO - Antoine ELIAS
4 * Copyright (C) 2014 - Scilab Enterprises - Anais AUBERT
6 * This file must be used under the terms of the CeCILL.
7 * This source file is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at
10 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13 /*--------------------------------------------------------------------------*/
15 #include "signal_gw.hxx"
18 #include "internal.hxx"
19 #include "function.hxx"
20 #include "signalprocessingfunctions.hxx"
24 #include "localization.h"
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 Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "corr", 2, 5);
37 return types::Function::Error;
41 if (in[0]->isString())
43 types::String* pS = in[0]->getAs<types::String>();
44 if (pS->getSize() == 1 && pS->get(0)[0] == L'f')
46 //[cov,mean]=corr('fft',xmacro,[ymacro],n,sect)
47 types::InternalType* pXFunction = NULL;
48 types::InternalType* pYFunction = NULL;
67 //check input parameters
68 if (in.size() < 4 || in.size() > 5)
70 Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "corr", 4, 5);
71 return types::Function::Error;
75 int iPos = (int)(in.size() - 1);
76 if (in[iPos]->isDouble() == false || in[iPos]->getAs<types::Double>()->isScalar() == false)
78 Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), "corr", iPos + 1);
79 return types::Function::Error;
82 iOutSize = (int)in[iPos]->getAs<types::Double>()->get(0);
87 if (in[iPos]->isDouble() == false || in[iPos]->getAs<types::Double>()->isScalar() == false)
89 Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), "corr", iPos + 1);
90 return types::Function::Error;
93 iTotalSize = (int)in[iPos]->getAs<types::Double>()->get(0);
95 Signalprocessingfunctions* spFunctionsManager = new Signalprocessingfunctions(L"corr");
96 Signalprocessing::addSignalprocessingfunctions(spFunctionsManager);
99 if (in[1]->isCallable())
101 pXFunction = in[1]->getAs<types::Callable>();
102 spFunctionsManager->setDgetx(in[1]->getAs<types::Callable>());
104 else if (in[1]->isString())
106 pXFunction = in[1]->getAs<types::String>();
107 spFunctionsManager->setDgetx(in[1]->getAs<types::String>());
111 Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), "corr", iPos + 1);
112 return types::Function::Error;
120 if (in[2]->isCallable())
122 pYFunction = in[2]->getAs<types::Callable>();
123 spFunctionsManager->setDgety(in[2]->getAs<types::Callable>());
125 else if (in[2]->isString())
127 pYFunction = in[2]->getAs<types::String>();
128 spFunctionsManager->setDgety(in[2]->getAs<types::String>());
132 Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), "corr", iPos + 2);
133 return types::Function::Error;
139 xa = new double[iSect];
140 xr = new double[iSect];
141 xi = new double[iSect];
142 zr = new double[iSect / 2 + 1];
143 zi = new double[iSect / 2 + 1];
144 C2F(cmpse2)(&iSect, &iTotalSize, &iMode, (void*) dgetx_f, (void*) dgety_f, xa, xr, xi, zr, zi, &iErr);
154 Scierror(999, _("fft call : needs power of two!"));
155 return types::Function::Error;
158 types::Double *pDblOut1 = new types::Double(1, iOutSize);
161 out.push_back(pDblOut1);
163 types::Double *pDblOut2 = new types::Double(1, iMode - 1);
166 out.push_back(pDblOut2);
168 return types::Function::OK;
170 else if (pS->getSize() == 1 && pS->get(0)[0] == L'u')
177 Scierror(999, _("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"), "corr", 1, "'fft', 'update'");
178 return types::Function::Error;
183 //usual case [cov,mean]=corr(x,[y],nlags)
185 int iCorrelation = 0;
186 types::Double* pDblX = NULL;
187 types::Double* pDblY = NULL;
188 types::Double* pDblCorrelation = NULL;
189 types::Double* pDblMean = NULL;
193 //check input parameters
194 if (in.size() < 2 || in.size() > 3)
196 Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "corr", 2, 3);
197 return types::Function::Error;
200 //get last parameter nlags
201 int iPos = (int)(in.size() - 1);
202 if (in[iPos]->isDouble() == false || in[iPos]->getAs<types::Double>()->isScalar() == false)
204 Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), "corr", iPos + 1);
205 return types::Function::Error;
208 iCorrelation = (int)in[iPos]->getAs<types::Double>()->get(0);
212 if (in[1]->isDouble() == false)
214 Scierror(999, _("%s: Wrong type for input argument #%d: Matrix expected.\n"), "corr" , 2);
215 return types::Function::Error;
218 pDblY = in[1]->getAs<types::Double>();
220 if (in[0]->isDouble() == false)
222 Scierror(999, _("%s: Wrong type for input argument #%d: Matrix expected.\n"), "corr" , 1);
223 return types::Function::Error;
226 pDblX = in[0]->getAs<types::Double>();
228 if (pDblX->getSize() != pDblY->getSize())
230 Scierror(60, _("%s: Wrong size for argument: Incompatible dimensions.\n"), "corr");
231 return types::Function::Error;
236 if (in[0]->isDouble() == false)
238 Scierror(999, _("%s: Wrong type for input argument #%d: Matrix expected.\n"), "corr" , 1);
239 return types::Function::Error;
242 pDblX = in[0]->getAs<types::Double>();
246 iSize = pDblX->getSize();
247 pDblCorrelation = new types::Double(1, iCorrelation);
248 C2F(tscccf)(pDblX->get(), pDblY->get(), &iSize, pDblCorrelation->get(), pdblMean, &iCorrelation, &iErr);
251 delete pDblCorrelation;
252 Scierror(999, _("%s: Too many coefficients are required.\n"), "corr");
253 return types::Function::Error;
256 out.push_back(pDblCorrelation);
262 pDblMean = new types::Double(1, 2);
266 pDblMean = new types::Double(1, 1);
269 pDblMean->set(pdblMean);
270 out.push_back(pDblMean);
274 return types::Function::OK;