2016-06-07 3 views
0

Я хотел бы выполнить поэлементно умножение двух массивов, оба типа сложны, но я получаю следующее сообщение об ошибке:поэлементно умножение двух массивов в C/C++

[email protected]:~/Downloads/OpenCV/opencv-2.4.9/build$ g++ -o myfft myfft.cpp -std=c++14 
In file included from myfft.cpp:14:0: 
cs_delay.cpp: In function ‘void cs_delay(CArray&, int, int, int)’: 
cs_delay.cpp:35:28: error: no match for ‘operator*’ (operand types are ‘void’ and ‘Complex {aka std::complex}’) 
     x[j] = ifft(fft(x) * rot[j]); 
          ^
cs_delay.cpp:35:28: note: candidates are: 
In file included from myfft.cpp:1:0: 
/usr/include/c++/4.9/complex:381:5: note: template std::complex std::operator*(const std::complex&, const std::complex&) 
    operator*(const complex& __x, const complex& __y) 
    ^
/usr/include/c++/4.9/complex:381:5: note: template argument deduction/substitution failed: 
In file included from myfft.cpp:14:0: 
cs_delay.cpp:35:35: note: mismatched types ‘const std::complex’ and ‘void’ 
     x[j] = ifft(fft(x) * rot[j]); 
           ^
In file included from myfft.cpp:1:0: 
/usr/include/c++/4.9/complex:390:5: note: template std::complex std::operator*(const std::complex&, const _Tp&) 
    operator*(const complex& __x, const _Tp& __y) 
    ^
/usr/include/c++/4.9/complex:390:5: note: template argument deduction/substitution failed: 
In file included from myfft.cpp:14:0: 
cs_delay.cpp:35:35: note: mismatched types ‘const std::complex’ and ‘void’ 
     x[j] = ifft(fft(x) * rot[j]); 
           ^
In file included from myfft.cpp:1:0: 
/usr/include/c++/4.9/complex:399:5: note: template std::complex std::operator*(const _Tp&, const std::complex&) 
    operator*(const _Tp& __x, const complex& __y) 
    ^
/usr/include/c++/4.9/complex:399:5: note: template argument deduction/substitution failed: 
In file included from myfft.cpp:14:0: 
cs_delay.cpp:35:35: note: deduced conflicting types for parameter ‘_Tp’ (‘void’ and ‘double’) 
     x[j] = ifft(fft(x) * rot[j]); 
           ^
In file included from /usr/include/c++/4.9/valarray:587:0, 
       from myfft.cpp:3: 
/usr/include/c++/4.9/bits/valarray_after.h:404:5: note: template std::_Expr, typename std::__fun::result_type> std::operator*(const std::_Expr&, const std::_Expr&) 
    _DEFINE_EXPR_BINARY_OPERATOR(*, __multiplies) 
    ^
/usr/include/c++/4.9/bits/valarray_after.h:404:5: note: template argument deduction/substitution failed: 
In file included from myfft.cpp:14:0: 
cs_delay.cpp:35:35: note: mismatched types ‘const std::_Expr’ and ‘void’ 
     x[j] = ifft(fft(x) * rot[j]); 
           ^
In file included from /usr/include/c++/4.9/valarray:587:0, 
       from myfft.cpp:3: 
/usr/include/c++/4.9/bits/valarray_after.h:404:5: note: template std::_Expr, typename std::__fun::result_type> std:

Функция, которая возвращает ошибку :

//cs_delay.cpp 
using namespace std; 

typedef std::complex<double> Complex; 
typedef std::valarray<Complex> CArray; 


void cs_delay(CArray& x, int rate_hz, int delay_s, int n) 
{ 

const size_t N = x.size(); 
    if (N <= 1) return; 

int j; 
double cycLen_s; 
double nCyc; 
double* f = new double[n]; 
double* phase = new double[n]; 
Complex* rot = new Complex[n]; 
cycLen_s = n/rate_hz; 
nCyc = delay_s/cycLen_s; 
/*************************************************************/ 
for (j = 0 ; j < n ; j++){ 

     f[j] =j+floor(n/2); 
     f[j] =fmod(f[j], n); 
     f[j] =f[j]-floor(n/2); 
     phase[j] = -2 * PI * f[j] * nCyc; 
     rot[j] = exp(1i*phase[j]); 
     std::cout << "rot["<<j<<"] ="<<rot[j] <<std::endl; 
     fft(x); 
     x *= rot[j]; 
     ifft(x); 
     } 
/*************************************************************/ 
     delete [] f; 
     delete [] phase; 
     delete [] rot; 
} 

FFT и IFFT здесь:

//fft.cpp 
using namespace std; 
const double PI = 3.141592653589793238460; 


//functions declarations 

typedef std::complex<double> Complex; 
typedef std::valarray<Complex> CArray; 
// Cooley–Tukey FFT (in-place, divide-and-conquer) 
// Higher memory requirements and redundancy although more intuitive 
void fft(CArray& x) 
{ 
    const size_t N = x.size(); 
    if (N <= 1) return; 

    // divide 
    CArray even = x[std::slice(0, N/2, 2)]; 
    CArray odd = x[std::slice(1, N/2, 2)]; 

    // conquer 
    fft(even); 
    fft(odd); 

    // combine 
    for (size_t k = 0; k < N/2; ++k) 
    { 
     Complex t = std::polar(1.0, -2 * PI * k/N) * odd[k]; 
     x[k ] = even[k] + t; 
     x[k+N/2] = even[k] - t; 
    } 
} 
// inverse fft (in-place) 
void ifft(CArray& x) 

{ 
    // conjugate the complex numbers 
    x = x.apply(std::conj); 

    // forward fft 
    fft(x); 

    // conjugate the complex numbers again 
    x = x.apply(std::conj); 

    // scale the numbers 
    x /= x.size(); 
} 

вот моя главная функция:

#include <complex> 
#include <iostream> 
#include <valarray> 
#include <malloc.h> 
#include <string> 
#include <stdlib.h> 
#include <fstream> 
#include <cstdio> 
#include <cstdlib> 
#include <cmath> 
#include <iomanip> 
#include <cmath> 
#include "fft.cpp" 
#include "cs_delay.cpp" 
using namespace std; 
//const double PI = 3.141592653589793238460; 
char filename[] = "vidres6.txt"; 
char filename2[] = "vidres7.txt"; 

//typedef std::complex<double> Complex; 
//typedef std::valarray <Complex> CArray; 
/*********************************************************************************************** 
*         function declarations 
************************** ******************************************************** ***********/ 
void fft(CArray& x); 
void ifft(CArray& x); 
void binFreq(int n); 
void cs_delay(CArray& x, int rate_hz, int delay_s, int n); 

int main() 

{ 
     int dTest_samples; 
     int cTest; 
     //cTest = -cTest; 
     int n=299; 
     int i; 
     int j; 
     double x [n]; 

    /*****************************getting x*******************************/ 

     string line; 
     double Result; 
      ifstream myfile (filename); 
      if (myfile.is_open()) 
       { 
       for (i = 0 ; (i < n) && (myfile >> x[i]) ; ++i) 

         cout << line << '\n'; 
         stringstream convert(line); 

         if (!(convert >> Result)) 
         Result = 0; 
         x[i]=Result; 


       } 
       else cout << "Unable to open file"; 
    /***********************************************************************/ 


    Complex test[n]; 

    for (i = 0 ; i < n ; ++i) 
    test[i] = x[i]; 

    CArray data(test,n); 

    // forward fft 
    fft(data); 

    std::cout << "fft" << std::endl; 
    for (int i = 0; i <n; ++i) 
    { 
     cout << data[i] << endl; 
    } 

    // inverse fft 
    ifft(data); 

    std::cout << std::endl << "ifft" << std::endl; 
    for (int i = 0; i <n; ++i) 
    { 
     std::cout << data[i] << std::endl; 
    } 

    return 0; 
} 

ответ

0

fft ничего не возвращает (он возвращает void), но вы пытаетесь умножить результат на что-то («fft(x) * rot[j]»). Это не сработает.

В той же строке вы назначаете результат ifftx[j], но ifft также ничего не возвращает.

Предполагая, что fft и ifft изменяют свои аргументы в месте правильно, попробуйте заменить строку

x[j] = ifft(fft(x) * rot[j]); 

по

fft(x); 
x *= rot[j]; 
ifft(x); 
+0

Я сделал изменения, и теперь я получаю это: cs_delay. cpp: В функции 'void cs_delay (CArray &, int, int, int)': cs_delay.cpp: 32: 16: ошибка: недействительная инициализация неконстантной ссылки типа «CArray & {aka std :: valarray > &} 'из n rvalue типа 'std :: _ Expr , std :: complex >, std :: complex >' ifft (x * rot [j]); ^ В файле включен из myfft.cpp: 13: 0: fft.cpp: 46: 6: Примечание: при прохождении аргумент 1 из 'пустот IFFT (CArray &)' пустот IFFT (CArray & х) – Serge

+0

@Serge: Ах , потому что 'x * rot [j]' является временным и не может быть передан как неконстантная ссылка. Попробуйте это изменение. – Kundor

+0

Это похоже на работу, я больше не получаю ошибку! В последнем случае, как я мог получить результат ifft (x), чтобы впоследствии использовать его в своей программе? , Я думал о чем-то вроде x = ifft (x), могу ли я это сделать? – Serge

Смежные вопросы