convolution (complex)

Computes the convolution, and optionally, the correlation of two complex vectors.

Synopsis

#include <imsl.h>

f_complex *imsl_c_convolution (int nx, f_complex x[], int ny, f_complex y[], int *nz, , 0)

The type d_complex function is imsl_z_convolution.

Required Arguments

int nx (Input)
Length of the vector x.

f_complex x[] (Input)
Real vector of length nx.

int ny (Input)
Length of the vector y.

f_complex y[] (Input)
Real vector of length ny.

int *nz (Output)
Length of the output vector.

Return Value

A pointer to an array of length nz containing the convolution of x and y. To release this space, use imsl_free. If no solution can be computed, then NULL is returned.

Synopsis with Optional Arguments

#include <imsl.h>

f_complex *imsl_c_convolution (int nx, f_complex x[], int ny, f_complex y[], int *nz,

IMSL_PERIODIC,

IMSL_CORRELATION,

IMSL_FIRST_CALL,

IMSL_CONTINUE_CALL,

IMSL_LAST_CALL,

IMSL_RETURN_USER, f_complex z[],

IMSL_Z_TRANS, f_complex **zhat

IMSL_Z_TRANS_USER, f_complex *zhat,

0)

Optional Arguments

IMSL_PERIODIC
The input is periodic.

IMSL_CORRELATION
Return the correlation of x and y.

IMSL_FIRST_CALL
If the function is called multiple times with the same nx and ny, select this option on the first call.

IMSL_CONTINUE_CALL
If the function is called multiple times with the same nx and ny, select this option on intermediate calls.

IMSL_LAST_CALL
If the function is called multiple times with the same nx and ny, select this option on the final call.

IMSL_RETURN_USER, f_complex z[] (Output)
User-supplied array of length at least nz containing the convolution or correlation of x and y.

IMSL_Z_TRANS, f_complex **zhat[] (Output)
Address of a pointer to an array of length at least nz containing on output the discrete Fourier transform of z.

IMSL_Z_TRANS_USER, f_complex zhat[] (Output)
User-supplied array of length at least nz containing on output the discrete Fourier transform of z.

Description

The function imsl_c_convolution, by default, computes the discrete convolution of two sequences x and y. More precisely, let nx be the length of x, and ny denote the length of y. If a circular convolution is desired, the optional argument IMSL_PERIODIC must be selected. We set

nz = max {ny, nx}

and we pad out the shorter vector with zeros. Then, we compute

 

where the index on x is interpreted as a positive number between 1 and nz, modulo nz.

The technique used to compute the zi’s is based on the fact that the (complex discrete) Fourier transform maps convolution into multiplication. Thus, the Fourier transform of z is given by

 

where the following equation is true.

 

The technique used here to compute the convolution is to take the discrete Fourier transform of x and y, multiply the results together component-wise, and then take the inverse transform of this product. It is very important to make sure that nzis the product of small primes if option IMSL_PERIODIC is selected. If nzis a product of small primes, then the computational effort will be proportional to nzlog (nz). If option IMSL_PERIODIC is not selected, then a good value is chosen for nz so that the Fourier transforms are efficient and nz  nx + ny  1. This will mean that both vectors will be padded with zeros.

Optionally, the function imsl_c_convolution computes the discrete correlation of two sequences x and y. More precisely, let n be the length of x and y. If a circular correlation is desired, then option IMSL_PERIODIC must be selected. We set (on output)

nz = n

if IMSL_PERIODIC is chosen

(nz = 2α3β5γ 2n  1)

if IMSL_PERIODIC is not chosen

where α, β, and γ are nonnegative integers yielding the smallest number of the type 2α3β5γ satisfying the inequality. Once nz is determined, we pad out the vectors with zeros. Then, we compute

 

where the index on x is interpreted as a positive number between one and nz, modulo nz. Note that this means that

 

contains the correlation of x (k  1) with y as k = 0, 1, , nz2. Thus, if x(k  1) = y(k) for all k, then we would expect

 

to be the largest component of z. The technique used to compute the zi’s is based on the fact that the (complex discrete) Fourier transform maps correlation into multiplication.

Thus, the Fourier transform of z is given by

 

where the following equation is true.

 

Thus, the technique used here to compute the correlation is to take the discrete Fourier transform of x and the conjugate of the discrete Fourier transform of y, multiply the results together component-wise, and then take the inverse transform of this product. It is very important to make sure that nz is the product of small primes if IMSL_PERIODIC is selected. If nzis the product of small primes, then the computational effort will be proportional to nzlog (nz). If IMSL_PERIODIC is not chosen, then a good value is chosen for nz so that the Fourier transforms are efficient and nz  2n  1. This will mean that both vectors will be padded with zeros.

No complex transforms of x or y are taken since both sequences are real, and real transforms can simulate the complex transform above. Such a strategy is six times faster and requires less space than when using the complex transform.

Examples

 

Example 1

This example computes a nonperiodic convolution. The purpose is to compute a moving average of the type found in digital filtering. The averaging operator in this case is especially simple and is given by averaging five consecutive points in the sequence. We try to recover the values of an exponential function contaminated by noise. The large error for the last value has to do with the fact that the convolution is averaging the zeros in the “pad” rather than the function values. Notice that the signal size is 100, but only report the errors at ten points.

 

#include <imsl.h>

#include <stdio.h>

#include <math.h>

 

#define NFLTR 5

#define NY 100

 

#define F1(A) (imsl_c_mul(imsl_cf_convert(exp(A),0.0), \

imsl_cf_convert(cos(A),sin(A)) ))

 

int main()

{

int i, nz;

f_complex fltr[NFLTR], temp,

y[NY], *z;

float x, twopi, total1, total2, *noise, origer, fltrer;

 

/* Set up the filter */

for (i = 0; i < NFLTR; i++) fltr[i] = imsl_cf_convert(0.2,0.0);

 

/* Set up y-vector for the periodic case */

twopi = 2.0*imsl_f_constant ("Pi", 0);

imsl_random_seed_set(1234579);

noise = imsl_f_random_uniform(2*NY, 0);

 

for (i = 0; i < NY; i++) {

x = (float)(i) / (NY - 1);

temp = imsl_cf_convert(0.5*noise[i]-0.25, 0.5*noise[NY+i]-0.25);

y[i] = imsl_c_add(F1(x), temp);

}

 

/* Call the convolution routine for the periodic case */

z = imsl_c_convolution(NFLTR, fltr, NY, y, &nz, 0);

 

/* Print results */

printf(" Periodic Case\n");

printf(" x F1(x) Original Error");

printf(" Filtered Error\n");

 

total1 = 0.0;

total2 = 0.0;

for (i = 0; i < NY; i++) {

x = (float)(i) / (NY - 1);

origer = imsl_c_abs(imsl_c_sub(y[i],F1(x)));

fltrer = imsl_c_abs(imsl_c_sub(z[i+2],F1(x)));

if ((i % 11) == 0)

printf(" %10.4f (%6.4f,%6.4f) %12.4f %15.4f\n",

x, (F1(x)).re, (F1(x)).im, origer, fltrer);

total1 += origer;

total2 += fltrer;

}

 

printf(" Average absolute error before filter:%10.5f\n",

total1 / (NY));

printf(" Average absolute error after filter:%11.5f\n",

total2 / (NY));

}

Output

 

Periodic Case

x F1(x) Original Error Filtered Error

0.0000 (1.0000,0.0000) 0.1684 0.3524

0.1111 (1.1106,0.1239) 0.0582 0.0822

0.2222 (1.2181,0.2752) 0.1991 0.1054

0.3333 (1.3188,0.4566) 0.1487 0.1001

0.4444 (1.4081,0.6706) 0.2381 0.1004

0.5556 (1.4808,0.9192) 0.1037 0.0708

0.6667 (1.5307,1.2044) 0.1312 0.0904

0.7778 (1.5508,1.5273) 0.1695 0.0856

0.8889 (1.5331,1.8885) 0.1851 0.0698

1.0000 (1.4687,2.2874) 0.2130 1.0760

Average absolute error before filter: 0.19057

Average absolute error after filter: 0.10024

Example 2

This example computes both a periodic correlation between two distinct signals x and y. There are 100 equally spaced points on the interval [0, 2π] and f1 (x) = cos (x) + i sin (x). Define x and y as follows:

 

Note that the maximum value of z (the correlation of x with) occurs at i = 25, which corresponds to the offset.

 

#include <imsl.h>

#include <math.h>

#include <stdio.h>

 

#define N 100

 

/* Define function */

#define F1(A) imsl_cf_convert(cos(A),sin(A))

 

int main()

{

int i, k, nz;

float zreal[4*N], pi, max, xnorm, ynorm, sumx, sumy;

f_complex x[N], y[N], *z;

 

/* Set up y-vector for the nonperiodic case */

pi = imsl_f_constant ("Pi", 0);

 

for (i = 0; i < N; i++) {

x[i] = F1(2.0*pi*(float)(i) / (N-1));

y[i] = F1(2.0*pi*(float)(i) / (N-1) + pi/2.0);

}

 

/* Call the correlation function for the

nonperidic case */

z = imsl_c_convolution(N, x, N, y, &nz,

IMSL_CORRELATION, IMSL_PERIODIC,0);

 

sumx = sumy = 0.0;

for (i = 0; i < N; i++) {

sumx += imsl_c_abs(imsl_c_mul(x[i], x[i]));

sumy += imsl_c_abs(imsl_c_mul(y[i], y[i]));

}

 

xnorm = sqrt((sumx));

ynorm = sqrt((sumy));

for (i = 0; i < N; i++) {

zreal[i] = (z[i].re/(xnorm*ynorm));

}

 

max = zreal[0];

k = 0;

 

for (i = 1; i < N; i++) {

if (max < zreal[i]) {

max = zreal[i];

k = i;

}

}

 

printf("The element of Z with the largest normalized\n");

printf("value is Z(%2d).\n", k);

printf("The normalized value of Z(%2d) is %6.3f\n", k, zreal[k]);

}

Output

 

The element of Z with the largest normalized

value is Z(25).

The normalized value of Z(25) is 1.000