Open In App

Fast convolution for 64-bit integers

Convolution is a mathematical operation used in signal processing, image processing, and other fields to combine two functions in order to produce a third function. It is defined as the integral of the product of two functions, one of which is flipped and shifted over time. It is often represented using the symbol “*” and is useful for filtering, smoothing, and other operations on signals and images.

Fast Convolution:

The convolution operation is commonly represented by the symbol ‘*’ and can be represented mathematically as:



c[n] = sum(a[m] * b[n – m]) for m = 0 to n

Steps involved in the implementation of the code:



Below is the code for the above approach:




// C++ implementation of the above approach
#include <cmath>
#include <iostream>
using namespace std;
 
// Function to calculate the convolution
// of two functions
double convolution(double signal[], double filter[],
                   int size, int size_of_filter)
{
 
    double result = 0;
    for (int i = 0; i < size; i++) {
        int ind = size - i - 1;
        if (ind >= 0 & ind < size_of_filter) // we need to check if filter array does not go out of bounds
                                              //otherwise we take as 0
            result += signal[i] * filter[size - 1 - i];
    }
    return result;
}
 
// Driver code
int main()
{
 
    // Define the signal and filter
    double signal[] = { 1, 2, 3, 4, 5 };
    double filter[] = { 0.1, 0.2, 0.3 };
    int size = sizeof(signal) / sizeof(signal[0]);
    int size_of_filter = sizeof(filter) / sizeof(filter[0]);
 
    // Calculate the convolution
    double conv_result = convolution(signal, filter, size, size_of_filter);
 
    // Print the result
    cout << "Convolution result: " << conv_result << endl;
 
    return 0;
}




import java.util.*;
 
public class Main {
 
    // Function to calculate the convolution
    // of two functions
    public static double convolution(double[] signal, double[] filter, int size, int size_of_filter) {
        double result = 0;
        for (int i = 0; i < size; i++) {
            int ind = size - i - 1;
            if (ind >= 0 && ind < size_of_filter) {
                // we need to check if filter array does not go out of bounds
                // otherwise we take as 0
                result += signal[i] * filter[size - 1 - i];
            }
        }
        return result;
    }
 
    // Driver code
    public static void main(String[] args) {
 
        // Define the signal and filter
        double[] signal = { 1, 2, 3, 4, 5 };
        double[] filter = { 0.1, 0.2, 0.3 };
        int size = signal.length;
        int size_of_filter = filter.length;
 
        // Calculate the convolution
        double conv_result = convolution(signal, filter, size, size_of_filter);
 
        // Print the result
        System.out.println("Convolution result: " + conv_result);
    }
}




def convolution(signal, filter):
    size = len(signal)
    size_of_filter = len(filter)
 
    result = 0
    for i in range(size):
        ind = size - i - 1
        if ind >= 0 and ind < size_of_filter: # we need to check if filter array does not go out of bounds
            #otherwise we take as 0
            result += signal[i] * filter[size - 1 - i]
    return result
 
# Driver code
if __name__ == '__main__':
 
    # Define the signal and filter
    signal = [1, 2, 3, 4, 5]
    filter = [0.1, 0.2, 0.3]
 
    # Calculate the convolution
    conv_result = convolution(signal, filter)
 
    # Print the result
    print("Convolution result: ", conv_result)




function convolution(signal, filter) {
    const size = signal.length;
    const size_of_filter = filter.length;
 
    let result = 0;
    for (let i = 0; i < size; i++) {
        const ind = size - i - 1;
        if (ind >= 0 && ind < size_of_filter) { // we need to check if filter array does not go out of bounds
            //otherwise we take as 0
            result += signal[i] * filter[size - 1 - i];
        }
    }
    return result;
}
 
// Driver code
const signal = [1, 2, 3, 4, 5];
const filter = [0.1, 0.2, 0.3];
 
// Calculate the convolution
const conv_result = convolution(signal, filter);
 
// Print the result
console.log("Convolution result: ", conv_result);




// C# implementation of the above approach
 
using System;
 
public class GFG {
    // Function to calculate the convolution
    // of two functions
    static double Convolution(double[] signal,
                              double[] filter, int size,
                              int size_of_filter)
    {
        double result = 0;
        for (int i = 0; i < size; i++) {
            int ind = size - i - 1;
            if (ind >= 0
                & ind < size_of_filter) // we need to check
                                        // if filter array
                                        // does not go out
                                        // of bounds
                                        // otherwise we take
                                        // as 0
                result += signal[i] * filter[size - 1 - i];
        }
        return result;
    }
 
    // Driver code
    public static void Main()
    {
        // Define the signal and filter
        double[] signal = { 1, 2, 3, 4, 5 };
        double[] filter = { 0.1, 0.2, 0.3 };
        int size = signal.Length;
        int size_of_filter = filter.Length;
 
        // Calculate the convolution
        double conv_result = Convolution(
            signal, filter, size, size_of_filter);
 
        // Print the result
        Console.WriteLine("Convolution result: "
                          + conv_result);
    }
}

Output
Convolution result: 2.2

Time Complexity: O(n).

Space Complexity: O(1) as no extra space has been used.

Popular Algorithms for Fast convolution are:

Fast Fourier Transform (FFT) algorithm:

Karatsuba algorithm:

Karatsuba vs FFT algorithm:

Conclusion:


Article Tags :