TY - ET-D - 60 - DAAOA - Lab 2

You might also like

You are on page 1of 3

Name: Ruturaj Uttarwar Class: TY ET-D

GR No: 12020036 Roll No: 60

DAAOA Lab 2

Implementation of Convolution Sum Equation

Convolution: A convolution is an integral that expresses the amount of overlap of one function   as
it is shifted over another function  . It therefore "blends" one function with another.

Linear Convolution Function to be implemented:


y ( n )= ∑ x ( k ) .h (n−k )
k=−∞

MATLAB Code:

clear all;

close all;

%Taking input

x=input('enter X[n]: ');

h=input('Enter H[n]');

l1 = length(x);

l2 = length(h);

N = l1 + l2 -1;

%Convoultion

a=[];

for i=1:l2

g=x.*h(i);

a=[a:g];

end

fprintf("\n");

disp(a);

clinear = conv(x,h);

fprintf("Convolution of X[n] and H[n] : \n")

disp(clinear)

Result:
Name: Ruturaj Uttarwar Class: TY ET-D
GR No: 12020036 Roll No: 60

Time Complexity:
n lies from 0 to n-1 & i lies from 0 to n-1;
Therefore, Complexity = n*I = n2 = o (n2 ).

C++ code:

#include <bits/stdc++.h> //standard lib


using namespace std; //scope resolution
constexpr int MOD = 998244353;

// Function to generate a convolution


// array of two given arrays
void findConvolution(const vector<int>& a,
const vector<int>& b)
{
// Stores the size of arrays
int n = a.size(), m = b.size();

// Stores the final array


vector<long long> c(n + m - 1);

// Traverse the two given arrays


for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {

// Update the convolution array


c[i + j] += 1LL*(a[i] * b[j]) % MOD;
}
}

// Print the convolution array c[]


for (int k = 0; k < c.size(); ++k) {
c[k] %= MOD;
cout << c[k] << " ";
}
}
int main()
{
vector<int> A = { 1, 2 , 3 ,1 };
vector<int> B = { 1,1,1 };

findConvolution(A, B);
Name: Ruturaj Uttarwar Class: TY ET-D
GR No: 12020036 Roll No: 60

return 0;
}

Result:

Time Complexity:

The computational complexity of the general convolution algorithm is O(n^2).

You might also like