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 snippet is an implementation of deconvolution, which is a mathematical operation that reverses the effects of convolution.
-
print_vector
:This is a utility function that prints a vector of
int32_t
values in a formatted manner, enclosing them in square brackets and separating them by commas. The last element is printed without a trailing comma. -
deconvolution
:This is the main function that performs deconvolution. It takes two vectors,
a
andb
, as input and returns a new vector that is the result of deconvolvinga
byb
.-
The function initializes a result vector
result
with a size equal toa.size() - b.size() + 1
and initializes all its elements to 0. -
It then iterates through the result vector, with
result[n]
representing the nth element, and performs the following steps:- Sets
result[n]
toa[n]
. - Calculates the start index as
std::max((int) (n - b.size() + 1), 0)
to ensure it doesn't go below 0. - Iterates through the range
[start, n)
and subtractsresult[i] * b[n - i]
fromresult[n]
.
- Sets
-
Finally, it divides
result[n]
byb[0]
and returns the result vector.
-
-
main
:This is the entry point of the program.
- It defines three constant vectors:
h
,f
, andg
with pre-populated values. - It then calls
deconvolution
withg
andf
as arguments and prints the result by passing it toprint_vector
. - It follows by calling
deconvolution
again withg
andh
as arguments and prints the result.
- It defines three constant vectors:
Source code in the cpp programming language
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <vector>
void print_vector(const std::vector<int32_t>& list) {
std::cout << "[";
for ( uint64_t i = 0; i < list.size() - 1; ++i ) {
std::cout << list[i] << ", ";
}
std::cout << list.back() << "]" << std::endl;
}
std::vector<int32_t> deconvolution(const std::vector<int32_t>& a, const std::vector<int32_t>& b) {
std::vector<int32_t> result(a.size() - b.size() + 1, 0);
for ( uint64_t n = 0; n < result.size(); n++ ) {
result[n] = a[n];
uint64_t start = std::max((int) (n - b.size() + 1), 0);
for ( uint64_t i = start; i < n; i++ ) {
result[n] -= result[i] * b[n - i];
}
result[n] /= b[0];
}
return result;
}
int main() {
const std::vector<int32_t> h = { -8, -9, -3, -1, -6, 7 };
const std::vector<int32_t> f = { -3, -6, -1, 8, -6, 3, -1, -9, -9, 3, -2, 5, 2, -2, -7, -1 };
const std::vector<int32_t> g = { 24, 75, 71, -34, 3, 22, -45, 23, 245, 25, 52,
25, -67, -96, 96, 31, 55, 36, 29, -43, -7 };
std::cout << "h = "; print_vector(h);
std::cout << "deconvolution(g, f) = "; print_vector(deconvolution(g, f));
std::cout << "f = "; print_vector(f);
std::cout << "deconvolution(g, h) = "; print_vector(deconvolution(g, h));
}
You may also check:How to resolve the algorithm 100 doors step by step in the Ada programming language
You may also check:How to resolve the algorithm Arithmetic/Integer step by step in the Objeck programming language
You may also check:How to resolve the algorithm Percolation/Site percolation step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Hello world/Standard error step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Cholesky decomposition step by step in the Smalltalk programming language