0
votes

I have a complex vector (type double) and another vector (type double), which I would like to perform 2D convolution with. Most of the examples I've seen online are not for complex vectors, so I'm not sure how to go about implementing it.

These are my vectors:

vector<complex<double> > signal;  // length of 100
vector<double> filter;            // length of 101

my 'signal' vector looks like this...

25 + 0.0000i, -9.04508 + 18.3273i, -3.45492 - 8.388i... and so on

my 'filter' vector looks like this...

0, 2.56698e-09, 9.13094e-09, 1.14301e-08... and so on

Ideally I'd like to output in to a complex vector, type double and to be the same length as 'signal' So my output is:

vector<complex<double> > filtered_signal;

I'm still relatively inexperienced with C++ so any high-level tips/guidance on how to perform the calculation is appreciated.

1

1 Answers

1
votes

convolution formula can be found at https://www.mathworks.com/help/matlab/ref/conv.html

as you can see,

w(k)= sum_j( u(j)v(k−j+1) )

where sum with index j is in the range that keeps the indices valid on the two vectors. k is between 0 and m+n-1, where m = length(u) and n = length(v)

so if u(j) is complex and v(k) is real, the product is well defined.

std::vector<complex<double> > conv(const std::vector<complex<double> > &a, const std::vector<double> &b)
{
    std::vector<complex<double> > r(a.size()+b.size()-1);
    for (unsigned long i=0; i<r.size(); i++) {
        r[i] = 0;
        unsigned long s =(i+2-b.size()>0) ? i+2-b.size() : 0;
        for (unsigned long j=s ; j<min(a.size(),i+2) ; j++) {
            r[i] += a[j] * b[i-j+1];
        }
    }
    return r;
}