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
5 * Copyright (C) 2014 - Scilab Enterprises - Sylvain GENIN
7 * This file must be used under the terms of the CeCILL.
8 * This source file is licensed as described in the file COPYING, which
9 * you should have received as part of this distribution. The terms
10 * are also available at
11 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
14 /*--------------------------------------------------------------------------*/
16 #include "signal_gw.hxx"
19 #include "internal.hxx"
20 #include "function.hxx"
21 #include "signalprocessingfunctions.hxx"
25 #include "localization.h"
31 /*--------------------------------------------------------------------------*/
32 types::Function::ReturnValue sci_corr(types::typed_list &in, int _iRetCount, types::typed_list &out)
34 //check input parameters
35 if (in.size() < 2 || in.size() > 5)
37 Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "corr", 2, 5);
38 return types::Function::Error;
42 if (in[0]->isString())
44 types::String* pS = in[0]->getAs<types::String>();
45 if (pS->getSize() == 1 && pS->get(0)[0] == L'f')
47 //[cov,mean]=corr('fft',xmacro,[ymacro],n,sect)
48 types::InternalType* pXFunction = NULL;
49 types::InternalType* pYFunction = NULL;
68 //check input parameters
69 if (in.size() < 4 || in.size() > 5)
71 Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "corr", 4, 5);
72 return types::Function::Error;
76 int iPos = (int)(in.size() - 1);
77 if (in[iPos]->isDouble() == false || in[iPos]->getAs<types::Double>()->isScalar() == false)
79 Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), "corr", iPos + 1);
80 return types::Function::Error;
83 iOutSize = (int)in[iPos]->getAs<types::Double>()->get(0);
88 if (in[iPos]->isDouble() == false || in[iPos]->getAs<types::Double>()->isScalar() == false)
90 Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), "corr", iPos + 1);
91 return types::Function::Error;
94 iTotalSize = (int)in[iPos]->getAs<types::Double>()->get(0);
96 Signalprocessingfunctions* spFunctionsManager = new Signalprocessingfunctions(L"corr");
97 Signalprocessing::addSignalprocessingfunctions(spFunctionsManager);
100 if (in[1]->isCallable())
102 pXFunction = in[1]->getAs<types::Callable>();
103 spFunctionsManager->setDgetx(in[1]->getAs<types::Callable>());
105 else if (in[1]->isString())
107 pXFunction = in[1]->getAs<types::String>();
108 spFunctionsManager->setDgetx(in[1]->getAs<types::String>());
112 Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), "corr", iPos + 1);
113 return types::Function::Error;
121 if (in[2]->isCallable())
123 pYFunction = in[2]->getAs<types::Callable>();
124 spFunctionsManager->setDgety(in[2]->getAs<types::Callable>());
126 else if (in[2]->isString())
128 pYFunction = in[2]->getAs<types::String>();
129 spFunctionsManager->setDgety(in[2]->getAs<types::String>());
133 Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), "corr", iPos + 2);
134 return types::Function::Error;
140 xa = new double[iSect];
141 xr = new double[iSect];
142 xi = new double[iSect];
143 zr = new double[iSect / 2 + 1];
144 zi = new double[iSect / 2 + 1];
145 C2F(cmpse2)(&iSect, &iTotalSize, &iMode, (void*) dgetx_f, (void*) dgety_f, xa, xr, xi, zr, zi, &iErr);
155 Scierror(999, _("fft call : needs power of two!"));
156 return types::Function::Error;
159 types::Double *pDblOut1 = new types::Double(1, iOutSize);
162 out.push_back(pDblOut1);
164 types::Double *pDblOut2 = new types::Double(1, iMode - 1);
167 out.push_back(pDblOut2);
169 return types::Function::OK;
171 else if (pS->getSize() == 1 && pS->get(0)[0] == L'u')
173 types::Double* pDblIn1 = NULL;
174 types::Double* pDblIn2 = NULL;
175 types::Double* pDblIn3 = NULL;
176 types::Double* pDblIn4 = NULL;
194 if (in[1]->isDouble() == false)
196 Scierror(999, _("%s: Wrong type for input argument #%d: Matrix expected.\n"), "corr" , 2);
197 return types::Function::Error;
200 pDblIn1 = in[1]->getAs<types::Double>();
201 if (pDblIn1->isComplex())
203 Scierror(999, _("%s: Wrong type for input argument #%d: Real matrix expected.\n"), "corr" , 2);
204 return types::Function::Error;
207 mnx = pDblIn1->getRows() * pDblIn1->getCols();
212 if (in[2]->isDouble() == false)
214 Scierror(999, _("%s: Wrong type for input argument #%d: Matrix expected.\n"), "corr" , 3);
215 return types::Function::Error;
218 pDblIn2 = in[2]->getAs<types::Double>();
220 mny = pDblIn2->getRows() * pDblIn2->getCols();
225 if (pDblIn2->isComplex())
227 Scierror(999, _("%s: Wrong type for input argument #%d: Real matrix expected.\n"), "corr" , 3);
228 return types::Function::Error;
237 if (pDblIn2->isComplex() == false)
239 double* wtempo = NULL;
240 w = new double[pDblIn2->getSize()];
241 wi = new double[mfft];
242 memset(wi, 0x00, sizeof(double) * mfft);
244 wtempo = pDblIn2->get();
245 memcpy(w, wtempo, sizeof(double) * pDblIn2->getSize());
249 double* wtempo = NULL;
250 double* witempo = NULL;
251 w = new double[pDblIn2->getSize()];
252 wi = new double[pDblIn2->getSize()];
254 wtempo = pDblIn2->getReal();
255 witempo = pDblIn2->getImg();
257 memcpy(w, wtempo, sizeof(double) * pDblIn2->getSize());
258 memcpy(wi, witempo, sizeof(double) * pDblIn2->getSize());
264 pDblIn3 = in[3]->getAs<types::Double>();
265 if (pDblIn3->isComplex())
267 Scierror(999, _("%s: Wrong type for input argument #%d: Real matrix expected.\n"), "corr" , 4);
268 return types::Function::Error;
271 xui = new double[mfft * 2];
272 double* xutempo = NULL;
273 xutempo = pDblIn3->get();
274 xu = new double[mfft * 2];
275 memset(xu, 0x00, sizeof(double) * mfft * 2);
276 memcpy(xu, xutempo, sizeof(double) * pDblIn3->getSize());
278 nbx = pDblIn3->getSize();
283 xu = new double[mfft * 2];
284 xui = new double[mfft * 2];
287 yi = new double[mny];
288 C2F(cmpse3)(&mfft, &mnx, &iMode, x, yi, xu, xui, w, wi, &iErr, &ichaud, &nbx);
295 Scierror(999, _("fft call : needs power of two!"));
296 return types::Function::Error;
302 pDblIn3 = in[3]->getAs<types::Double>();
303 mfft = pDblIn3->getRows() * pDblIn3->getCols();
304 if (pDblIn3->isComplex() == false)
306 wi = new double[mfft];
307 memset(wi, 0x00, sizeof(double) * mfft);
309 w = new double[pDblIn3->getSize()];
310 double* wtempo = NULL;
311 wtempo = pDblIn3->get();
312 memcpy(w, wtempo, sizeof(double) * pDblIn3->getSize());
317 double* wtempo = NULL;
318 double* witempo = NULL;
319 w = new double[pDblIn3->getSize()];
320 wi = new double[pDblIn3->getSize()];
322 wtempo = pDblIn3->getReal();
323 witempo = pDblIn3->getImg();
325 memcpy(w, wtempo, sizeof(double) * pDblIn3->getSize());
326 memcpy(wi, witempo, sizeof(double) * pDblIn3->getSize());
330 pDblIn4 = in[4]->getAs<types::Double>();
331 nbx = pDblIn4->getSize();
332 double* xutempo = NULL;
333 xutempo = pDblIn4->get();
334 xu = new double[mfft * 2];
335 memset(xu, 0x00, sizeof(double) * mfft * 2);
336 memcpy(xu, xutempo, sizeof(double) * pDblIn4->getSize());
339 xui = new double[mfft * 2];
343 xu = new double[mfft * 2];
344 xui = new double[mfft * 2];
347 C2F(cmpse3)(&mfft, &mnx, &iMode, x, y, xu, xui, w, wi, &iErr, &ichaud, &nbx);
354 Scierror(999, _("fft call : needs power of two!"));
355 return types::Function::Error;
360 types::Double *pDblOut1 = NULL;
361 pDblOut1 = new types::Double(1, mfft, true);
363 pDblOut1->setImg(wi);
364 out.push_back(pDblOut1);
368 types::Double *pDblOut2 = NULL;
369 pDblOut2 = new types::Double(1, mfft / 2);
371 for (int i = 0; i < mfft / 2; i++)
373 xui[i] = x[mnx - mfft / 2 + i];
377 out.push_back(pDblOut2);
384 return types::Function::OK;
390 Scierror(999, _("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"), "corr", 1, "'fft', 'update'");
391 return types::Function::Error;
396 //usual case [cov,mean]=corr(x,[y],nlags)
398 int iCorrelation = 0;
399 types::Double* pDblX = NULL;
400 types::Double* pDblY = NULL;
401 types::Double* pDblCorrelation = NULL;
402 types::Double* pDblMean = NULL;
406 //check input parameters
407 if (in.size() < 2 || in.size() > 3)
409 Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "corr", 2, 3);
410 return types::Function::Error;
413 //get last parameter nlags
414 int iPos = (int)(in.size() - 1);
415 if (in[iPos]->isDouble() == false || in[iPos]->getAs<types::Double>()->isScalar() == false)
417 Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), "corr", iPos + 1);
418 return types::Function::Error;
421 iCorrelation = (int)in[iPos]->getAs<types::Double>()->get(0);
425 if (in[1]->isDouble() == false)
427 Scierror(999, _("%s: Wrong type for input argument #%d: Matrix expected.\n"), "corr" , 2);
428 return types::Function::Error;
431 pDblY = in[1]->getAs<types::Double>();
433 if (in[0]->isDouble() == false)
435 Scierror(999, _("%s: Wrong type for input argument #%d: Matrix expected.\n"), "corr" , 1);
436 return types::Function::Error;
439 pDblX = in[0]->getAs<types::Double>();
441 if (pDblX->getSize() != pDblY->getSize())
443 Scierror(60, _("%s: Wrong size for argument: Incompatible dimensions.\n"), "corr");
444 return types::Function::Error;
449 if (in[0]->isDouble() == false)
451 Scierror(999, _("%s: Wrong type for input argument #%d: Matrix expected.\n"), "corr" , 1);
452 return types::Function::Error;
455 pDblX = in[0]->getAs<types::Double>();
459 iSize = pDblX->getSize();
460 pDblCorrelation = new types::Double(1, iCorrelation);
461 C2F(tscccf)(pDblX->get(), pDblY->get(), &iSize, pDblCorrelation->get(), pdblMean, &iCorrelation, &iErr);
464 delete pDblCorrelation;
465 Scierror(999, _("%s: Too many coefficients are required.\n"), "corr");
466 return types::Function::Error;
469 out.push_back(pDblCorrelation);
475 pDblMean = new types::Double(1, 2);
479 pDblMean = new types::Double(1, 1);
482 pDblMean->set(pdblMean);
483 out.push_back(pDblMean);
487 return types::Function::OK;