How to resolve the algorithm Deconvolution/1D step by step in the C programming language
How to resolve the algorithm Deconvolution/1D step by step in the C programming language
Table of Contents
Problem Statement
The convolution of two functions
F
{\displaystyle {\mathit {F}}}
and
H
{\displaystyle {\mathit {H}}}
of an integer variable is defined as the function
G
{\displaystyle {\mathit {G}}}
satisfying for all integers
n
{\displaystyle {\mathit {n}}}
. Assume
F ( n )
{\displaystyle F(n)}
can be non-zero only for
0
{\displaystyle 0}
≤
n
{\displaystyle {\mathit {n}}}
≤
|
F
|
{\displaystyle |{\mathit {F}}|}
, where
|
F
|
{\displaystyle |{\mathit {F}}|}
is the "length" of
F
{\displaystyle {\mathit {F}}}
, and similarly for
G
{\displaystyle {\mathit {G}}}
and
H
{\displaystyle {\mathit {H}}}
, so that the functions can be modeled as finite sequences by identifying
f
0
,
f
1
,
f
2
, …
{\displaystyle f_{0},f_{1},f_{2},\dots }
with
F ( 0 ) , F ( 1 ) , F ( 2 ) , …
{\displaystyle F(0),F(1),F(2),\dots }
, etc. Then for example, values of
|
F
|
= 6
{\displaystyle |{\mathit {F}}|=6}
and
|
H
|
= 5
{\displaystyle |{\mathit {H}}|=5}
would determine the following value of
g
{\displaystyle {\mathit {g}}}
by definition. We can write this in matrix form as: or For this task, implement a function (or method, procedure, subroutine, etc.) deconv to perform deconvolution (i.e., the inverse of convolution) by constructing and solving such a system of equations represented by the above matrix
A
{\displaystyle A}
for
h
{\displaystyle {\mathit {h}}}
given
f
{\displaystyle {\mathit {f}}}
and
g
{\displaystyle {\mathit {g}}}
.
h = [-8,-9,-3,-1,-6,7] f = [-3,-6,-1,8,-6,3,-1,-9,-9,3,-2,5,2,-2,-7,-1] g = [24,75,71,-34,3,22,-45,23,245,25,52,25,-67,-96,96,31,55,36,29,-43,-7]
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Deconvolution/1D step by step in the C programming language
The provided C code is an implementation of a fast Fourier transform (FFT) algorithm and a deconvolution function that utilizes the FFT to perform deconvolution. Let's break down the code step by step:
-
Header Files:
- The code includes several standard C libraries:
<stdio.h>
: Input/output operations<stdlib.h>
: Standard library functions likemalloc
<math.h>
: Mathematical functions<complex.h>
: Complex number operations
- The code includes several standard C libraries:
-
Constants and Types:
PI
is defined as the value of π (pi).cplx
is defined as a complex number type using thecomplex.h
library.
-
FFT Function:
fft
calculates the FFT of a complex arraybuf
of lengthn
.- It uses a recursive "_fft" function that employs the Cooley-Tukey algorithm to efficiently compute the FFT.
- The algorithm divides the array into smaller parts, computes the FFT of each part, and combines the results.
-
Padding Function:
pad_two
takes a real arrayg
of lengthlen
and pads it with zeros to a power of two lengthn
.- It returns a complex array
buf
with the padded data. - The padding ensures that the FFT is computed efficiently.
-
Deconvolution Function:
deconv
performs deconvolution between two input arrays,g
andf
, by converting them into the frequency domain using FFT.- It computes the quotient
h
in the frequency domain and converts it back to the time domain using inverse FFT. - The output
out
is shifted to align the result with the original lengths ofg
andf
.
-
Main Function:
- The
main
function defines sample arraysg
,f
, andh
for testing the deconvolution function. - It calculates
f
fromg
andh
using deconvolution and prints the results. - It also calculates
h
fromg
andf
using deconvolution and prints the results.
- The
In summary, this code provides an implementation of the FFT algorithm and uses it to perform deconvolution. Deconvolution is a mathematical operation that recovers a signal from its distorted version, and the FFT is used to efficiently perform this operation in the frequency domain.
Source code in the c programming language
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <complex.h>
double PI;
typedef double complex cplx;
void _fft(cplx buf[], cplx out[], int n, int step)
{
if (step < n) {
_fft(out, buf, n, step * 2);
_fft(out + step, buf + step, n, step * 2);
for (int i = 0; i < n; i += 2 * step) {
cplx t = cexp(-I * PI * i / n) * out[i + step];
buf[i / 2] = out[i] + t;
buf[(i + n)/2] = out[i] - t;
}
}
}
void fft(cplx buf[], int n)
{
cplx out[n];
for (int i = 0; i < n; i++) out[i] = buf[i];
_fft(buf, out, n, 1);
}
/* pad array length to power of two */
cplx *pad_two(double g[], int len, int *ns)
{
int n = 1;
if (*ns) n = *ns;
else while (n < len) n *= 2;
cplx *buf = calloc(sizeof(cplx), n);
for (int i = 0; i < len; i++) buf[i] = g[i];
*ns = n;
return buf;
}
void deconv(double g[], int lg, double f[], int lf, double out[]) {
int ns = 0;
cplx *g2 = pad_two(g, lg, &ns);
cplx *f2 = pad_two(f, lf, &ns);
fft(g2, ns);
fft(f2, ns);
cplx h[ns];
for (int i = 0; i < ns; i++) h[i] = g2[i] / f2[i];
fft(h, ns);
for (int i = 0; i >= lf - lg; i--)
out[-i] = h[(i + ns) % ns]/32;
free(g2);
free(f2);
}
int main()
{
PI = atan2(1,1) * 4;
double g[] = {24,75,71,-34,3,22,-45,23,245,25,52,25,-67,-96,96,31,55,36,29,-43,-7};
double f[] = { -3,-6,-1,8,-6,3,-1,-9,-9,3,-2,5,2,-2,-7,-1 };
double h[] = { -8,-9,-3,-1,-6,7 };
int lg = sizeof(g)/sizeof(double);
int lf = sizeof(f)/sizeof(double);
int lh = sizeof(h)/sizeof(double);
double h2[lh];
double f2[lf];
printf("f[] data is : ");
for (int i = 0; i < lf; i++) printf(" %g", f[i]);
printf("\n");
printf("deconv(g, h): ");
deconv(g, lg, h, lh, f2);
for (int i = 0; i < lf; i++) printf(" %g", f2[i]);
printf("\n");
printf("h[] data is : ");
for (int i = 0; i < lh; i++) printf(" %g", h[i]);
printf("\n");
printf("deconv(g, f): ");
deconv(g, lg, f, lf, h2);
for (int i = 0; i < lh; i++) printf(" %g", h2[i]);
printf("\n");
}
You may also check:How to resolve the algorithm The Twelve Days of Christmas step by step in the Java programming language
You may also check:How to resolve the algorithm Variable size/Set step by step in the Ada programming language
You may also check:How to resolve the algorithm Zumkeller numbers step by step in the Julia programming language
You may also check:How to resolve the algorithm Jacobi symbol step by step in the Java programming language
You may also check:How to resolve the algorithm I before E except after C step by step in the Maple programming language