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);
267 return types::Function::Error;
270 xui = new double[mfft * 2];
271 double* xutempo = NULL;
272 xutempo = pDblIn3->get();
273 xu = new double[mfft * 2];
274 memset(xu, 0x00, sizeof(double) * mfft * 2);
275 memcpy(xu, xutempo, sizeof(double) * pDblIn3->getSize());
277 nbx = pDblIn3->getSize();
282 xu = new double[mfft * 2];
283 xui = new double[mfft * 2];
286 yi = new double[mny];
287 C2F(cmpse3)(&mfft, &mnx, &iMode, x, yi, xu, xui, w, wi, &iErr, &ichaud, &nbx);
294 Scierror(999, _("fft call : needs power of two!"));
295 return types::Function::Error;
301 pDblIn3 = in[3]->getAs<types::Double>();
302 mfft = pDblIn3->getRows() * pDblIn3->getCols();
303 if (pDblIn3->isComplex() == false)
305 wi = new double[mfft];
306 memset(wi, 0x00, sizeof(double) * mfft);
308 w = new double[pDblIn3->getSize()];
309 double* wtempo = NULL;
310 wtempo = pDblIn3->get();
311 memcpy(w, wtempo, sizeof(double) * pDblIn3->getSize());
316 double* wtempo = NULL;
317 double* witempo = NULL;
318 w = new double[pDblIn3->getSize()];
319 wi = new double[pDblIn3->getSize()];
321 wtempo = pDblIn3->getReal();
322 witempo = pDblIn3->getImg();
324 memcpy(w, wtempo, sizeof(double) * pDblIn3->getSize());
325 memcpy(wi, witempo, sizeof(double) * pDblIn3->getSize());
329 pDblIn4 = in[4]->getAs<types::Double>();
330 nbx = pDblIn4->getSize();
331 double* xutempo = NULL;
332 xutempo = pDblIn4->get();
333 xu = new double[mfft * 2];
334 memset(xu, 0x00, sizeof(double) * mfft * 2);
335 memcpy(xu, xutempo, sizeof(double) * pDblIn4->getSize());
338 xui = new double[mfft * 2];
342 xu = new double[mfft * 2];
343 xui = new double[mfft * 2];
346 C2F(cmpse3)(&mfft, &mnx, &iMode, x, y, xu, xui, w, wi, &iErr, &ichaud, &nbx);
353 Scierror(999, _("fft call : needs power of two!"));
354 return types::Function::Error;
359 types::Double *pDblOut1 = NULL;
360 pDblOut1 = new types::Double(1, mfft, true);
362 pDblOut1->setImg(wi);
363 out.push_back(pDblOut1);
367 types::Double *pDblOut2 = NULL;
368 pDblOut2 = new types::Double(1, mfft / 2);
370 for (int i = 0; i < mfft / 2; i++)
372 xui[i] = x[mnx - mfft / 2 + i];
376 out.push_back(pDblOut2);
383 return types::Function::OK;
389 Scierror(999, _("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"), "corr", 1, "'fft', 'update'");
390 return types::Function::Error;
395 //usual case [cov,mean]=corr(x,[y],nlags)
397 int iCorrelation = 0;
398 types::Double* pDblX = NULL;
399 types::Double* pDblY = NULL;
400 types::Double* pDblCorrelation = NULL;
401 types::Double* pDblMean = NULL;
405 //check input parameters
406 if (in.size() < 2 || in.size() > 3)
408 Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "corr", 2, 3);
409 return types::Function::Error;
412 //get last parameter nlags
413 int iPos = (int)(in.size() - 1);
414 if (in[iPos]->isDouble() == false || in[iPos]->getAs<types::Double>()->isScalar() == false)
416 Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), "corr", iPos + 1);
417 return types::Function::Error;
420 iCorrelation = (int)in[iPos]->getAs<types::Double>()->get(0);
424 if (in[1]->isDouble() == false)
426 Scierror(999, _("%s: Wrong type for input argument #%d: Matrix expected.\n"), "corr" , 2);
427 return types::Function::Error;
430 pDblY = in[1]->getAs<types::Double>();
432 if (in[0]->isDouble() == false)
434 Scierror(999, _("%s: Wrong type for input argument #%d: Matrix expected.\n"), "corr" , 1);
435 return types::Function::Error;
438 pDblX = in[0]->getAs<types::Double>();
440 if (pDblX->getSize() != pDblY->getSize())
442 Scierror(60, _("%s: Wrong size for argument: Incompatible dimensions.\n"), "corr");
443 return types::Function::Error;
448 if (in[0]->isDouble() == false)
450 Scierror(999, _("%s: Wrong type for input argument #%d: Matrix expected.\n"), "corr" , 1);
451 return types::Function::Error;
454 pDblX = in[0]->getAs<types::Double>();
458 iSize = pDblX->getSize();
459 pDblCorrelation = new types::Double(1, iCorrelation);
460 C2F(tscccf)(pDblX->get(), pDblY->get(), &iSize, pDblCorrelation->get(), pdblMean, &iCorrelation, &iErr);
463 delete pDblCorrelation;
464 Scierror(999, _("%s: Too many coefficients are required.\n"), "corr");
465 return types::Function::Error;
468 out.push_back(pDblCorrelation);
474 pDblMean = new types::Double(1, 2);
478 pDblMean = new types::Double(1, 1);
481 pDblMean->set(pdblMean);
482 out.push_back(pDblMean);
486 return types::Function::OK;