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 * Copyright (C) 2012 - 2016 - Scilab Enterprises
9 * This file is hereby licensed under the terms of the GNU GPL v2.0,
10 * pursuant to article 5.3.4 of the CeCILL v.2.1.
11 * This file was originally licensed under the terms of the CeCILL v2.1,
12 * and continues to be available under such terms.
13 * For more information, see the COPYING file which you should have received
14 * along with this program.
17 /*--------------------------------------------------------------------------*/
19 #include "signal_gw.hxx"
22 #include "internal.hxx"
23 #include "function.hxx"
24 #include "signalprocessingfunctions.hxx"
28 #include "localization.h"
34 /*--------------------------------------------------------------------------*/
35 types::Function::ReturnValue sci_corr(types::typed_list &in, int _iRetCount, types::typed_list &out)
37 //check input parameters
38 if (in.size() < 2 || in.size() > 5)
40 Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "corr", 2, 5);
41 return types::Function::Error;
45 if (in[0]->isString())
47 types::String* pS = in[0]->getAs<types::String>();
48 if (pS->getSize() == 1 && pS->get(0)[0] == L'f')
50 //[cov,mean]=corr('fft',xmacro,[ymacro],n,sect)
69 //check input parameters
70 if (in.size() < 4 || in.size() > 5)
72 Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "corr", 4, 5);
73 return types::Function::Error;
77 int iPos = (int)(in.size() - 1);
78 if (in[iPos]->isDouble() == false || in[iPos]->getAs<types::Double>()->isScalar() == false)
80 Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), "corr", iPos + 1);
81 return types::Function::Error;
84 iOutSize = (int)in[iPos]->getAs<types::Double>()->get(0);
89 if (in[iPos]->isDouble() == false || in[iPos]->getAs<types::Double>()->isScalar() == false)
91 Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), "corr", iPos + 1);
92 return types::Function::Error;
95 iTotalSize = (int)in[iPos]->getAs<types::Double>()->get(0);
97 Signalprocessingfunctions* spFunctionsManager = new Signalprocessingfunctions(L"corr");
98 Signalprocessing::addSignalprocessingfunctions(spFunctionsManager);
101 if (in[1]->isCallable())
103 spFunctionsManager->setDgetx(in[1]->getAs<types::Callable>());
105 else if (in[1]->isString())
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 spFunctionsManager->setDgety(in[2]->getAs<types::Callable>());
124 else if (in[2]->isString())
126 spFunctionsManager->setDgety(in[2]->getAs<types::String>());
130 Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), "corr", iPos + 2);
131 return types::Function::Error;
137 xa = new double[iSect];
138 xr = new double[iSect];
139 xi = new double[iSect];
140 zr = new double[iSect / 2 + 1];
141 zi = new double[iSect / 2 + 1];
142 C2F(cmpse2)(&iSect, &iTotalSize, &iMode, (void*) dgetx_f, (void*) dgety_f, xa, xr, xi, zr, zi, &iErr);
152 Scierror(999, _("fft call : needs power of two!"));
153 return types::Function::Error;
156 types::Double *pDblOut1 = new types::Double(1, iOutSize);
159 out.push_back(pDblOut1);
161 types::Double *pDblOut2 = new types::Double(1, iMode - 1);
164 out.push_back(pDblOut2);
166 return types::Function::OK;
168 else if (pS->getSize() == 1 && pS->get(0)[0] == L'u')
170 types::Double* pDblIn1 = NULL;
171 types::Double* pDblIn2 = NULL;
172 types::Double* pDblIn3 = NULL;
173 types::Double* pDblIn4 = NULL;
191 if (in[1]->isDouble() == false)
193 Scierror(999, _("%s: Wrong type for input argument #%d: Matrix expected.\n"), "corr" , 2);
194 return types::Function::Error;
197 pDblIn1 = in[1]->getAs<types::Double>();
198 if (pDblIn1->isComplex())
200 Scierror(999, _("%s: Wrong type for input argument #%d: Real matrix expected.\n"), "corr" , 2);
201 return types::Function::Error;
204 mnx = pDblIn1->getRows() * pDblIn1->getCols();
209 if (in[2]->isDouble() == false)
211 Scierror(999, _("%s: Wrong type for input argument #%d: Matrix expected.\n"), "corr" , 3);
212 return types::Function::Error;
215 pDblIn2 = in[2]->getAs<types::Double>();
217 mny = pDblIn2->getRows() * pDblIn2->getCols();
222 if (pDblIn2->isComplex())
224 Scierror(999, _("%s: Wrong type for input argument #%d: Real matrix expected.\n"), "corr" , 3);
225 return types::Function::Error;
234 if (pDblIn2->isComplex() == false)
236 double* wtempo = NULL;
237 w = new double[pDblIn2->getSize()];
238 wi = new double[mfft];
239 memset(wi, 0x00, sizeof(double) * mfft);
241 wtempo = pDblIn2->get();
242 memcpy(w, wtempo, sizeof(double) * pDblIn2->getSize());
246 double* wtempo = NULL;
247 double* witempo = NULL;
248 w = new double[pDblIn2->getSize()];
249 wi = new double[pDblIn2->getSize()];
251 wtempo = pDblIn2->getReal();
252 witempo = pDblIn2->getImg();
254 memcpy(w, wtempo, sizeof(double) * pDblIn2->getSize());
255 memcpy(wi, witempo, sizeof(double) * pDblIn2->getSize());
261 pDblIn3 = in[3]->getAs<types::Double>();
262 if (pDblIn3->isComplex())
264 Scierror(999, _("%s: Wrong type for input argument #%d: Real matrix expected.\n"), "corr" , 4);
265 return types::Function::Error;
268 xui = new double[mfft * 2];
269 double* xutempo = NULL;
270 xutempo = pDblIn3->get();
271 xu = new double[mfft * 2];
272 memset(xu, 0x00, sizeof(double) * mfft * 2);
273 memcpy(xu, xutempo, sizeof(double) * pDblIn3->getSize());
275 nbx = pDblIn3->getSize();
280 xu = new double[mfft * 2];
281 xui = new double[mfft * 2];
284 yi = new double[mny];
285 C2F(cmpse3)(&mfft, &mnx, &iMode, x, yi, xu, xui, w, wi, &iErr, &ichaud, &nbx);
292 Scierror(999, _("fft call : needs power of two!"));
293 return types::Function::Error;
299 pDblIn3 = in[3]->getAs<types::Double>();
300 mfft = pDblIn3->getRows() * pDblIn3->getCols();
301 if (pDblIn3->isComplex() == false)
303 wi = new double[mfft];
304 memset(wi, 0x00, sizeof(double) * mfft);
306 w = new double[pDblIn3->getSize()];
307 double* wtempo = NULL;
308 wtempo = pDblIn3->get();
309 memcpy(w, wtempo, sizeof(double) * pDblIn3->getSize());
314 double* wtempo = NULL;
315 double* witempo = NULL;
316 w = new double[pDblIn3->getSize()];
317 wi = new double[pDblIn3->getSize()];
319 wtempo = pDblIn3->getReal();
320 witempo = pDblIn3->getImg();
322 memcpy(w, wtempo, sizeof(double) * pDblIn3->getSize());
323 memcpy(wi, witempo, sizeof(double) * pDblIn3->getSize());
327 pDblIn4 = in[4]->getAs<types::Double>();
328 nbx = pDblIn4->getSize();
329 double* xutempo = NULL;
330 xutempo = pDblIn4->get();
331 xu = new double[mfft * 2];
332 memset(xu, 0x00, sizeof(double) * mfft * 2);
333 memcpy(xu, xutempo, sizeof(double) * pDblIn4->getSize());
336 xui = new double[mfft * 2];
340 xu = new double[mfft * 2];
341 xui = new double[mfft * 2];
344 C2F(cmpse3)(&mfft, &mnx, &iMode, x, y, xu, xui, w, wi, &iErr, &ichaud, &nbx);
351 Scierror(999, _("fft call : needs power of two!"));
352 return types::Function::Error;
357 types::Double *pDblOut1 = NULL;
358 pDblOut1 = new types::Double(1, mfft, true);
360 pDblOut1->setImg(wi);
361 out.push_back(pDblOut1);
365 types::Double *pDblOut2 = NULL;
366 pDblOut2 = new types::Double(1, mfft / 2);
368 for (int i = 0; i < mfft / 2; i++)
370 xui[i] = x[mnx - mfft / 2 + i];
374 out.push_back(pDblOut2);
381 return types::Function::OK;
387 Scierror(999, _("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"), "corr", 1, "'fft', 'update'");
388 return types::Function::Error;
393 //usual case [cov,mean]=corr(x,[y],nlags)
395 int iCorrelation = 0;
396 types::Double* pDblX = NULL;
397 types::Double* pDblY = NULL;
398 types::Double* pDblCorrelation = NULL;
399 types::Double* pDblMean = NULL;
403 //check input parameters
404 if (in.size() < 2 || in.size() > 3)
406 Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "corr", 2, 3);
407 return types::Function::Error;
410 //get last parameter nlags
411 int iPos = (int)(in.size() - 1);
412 if (in[iPos]->isDouble() == false || in[iPos]->getAs<types::Double>()->isScalar() == false)
414 Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), "corr", iPos + 1);
415 return types::Function::Error;
418 iCorrelation = (int)in[iPos]->getAs<types::Double>()->get(0);
422 if (in[1]->isDouble() == false)
424 Scierror(999, _("%s: Wrong type for input argument #%d: Matrix expected.\n"), "corr" , 2);
425 return types::Function::Error;
428 pDblY = in[1]->getAs<types::Double>();
430 if (in[0]->isDouble() == false)
432 Scierror(999, _("%s: Wrong type for input argument #%d: Matrix expected.\n"), "corr" , 1);
433 return types::Function::Error;
436 pDblX = in[0]->getAs<types::Double>();
438 if (pDblX->getSize() != pDblY->getSize())
440 Scierror(60, _("%s: Wrong size for argument: Incompatible dimensions.\n"), "corr");
441 return types::Function::Error;
446 if (in[0]->isDouble() == false)
448 Scierror(999, _("%s: Wrong type for input argument #%d: Matrix expected.\n"), "corr" , 1);
449 return types::Function::Error;
452 pDblX = in[0]->getAs<types::Double>();
456 iSize = pDblX->getSize();
457 pDblCorrelation = new types::Double(1, iCorrelation);
458 C2F(tscccf)(pDblX->get(), pDblY->get(), &iSize, pDblCorrelation->get(), pdblMean, &iCorrelation, &iErr);
461 delete pDblCorrelation;
462 Scierror(999, _("%s: Too many coefficients are required.\n"), "corr");
463 return types::Function::Error;
466 out.push_back(pDblCorrelation);
472 pDblMean = new types::Double(1, 2);
476 pDblMean = new types::Double(1, 1);
479 pDblMean->set(pdblMean);
480 out.push_back(pDblMean);
484 return types::Function::OK;