0% found this document useful (0 votes)
5 views6 pages

Convolution in Python with Matplotlib

This document defines functions for convolving signals and filtering using different approaches. It generates and plots various intermediate and output signals to demonstrate and compare the approaches.

Uploaded by

ridhubaran
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Convolution in Python with Matplotlib

This document defines functions for convolving signals and filtering using different approaches. It generates and plots various intermediate and output signals to demonstrate and compare the approaches.

Uploaded by

ridhubaran
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import matplotlib.

pyplot as plt

import numpy as np

def sys(): # x[n] , h1[n] , h2[n] and h3[n] plotting

y1=[]

for i in range(-2,3):

[Link](i)

y2=[]

for i in range(3):

[Link](0.5)

y3=[0]*3

for i in range(4):

[Link](0)

[Link](0.25)

return y1,y2,y3

def convo(x,h): # Convolution Function

l=len(x)+len(h)-1

y=[0]*l

for i in range(len(h),l):

[Link](0)

for i in range(len(x)):

for j in range(len(y)):

z=shift(h,i)

y[j]+=x[i]*z[j]

return y

def con1(inp,y1,y2,y3): # Convolution using Approach 1

z=convo(inp,y1)

w=convo(z,y2)

v=convo(z,y3)
y=[]

for i in range(len(w)):

[Link](w[i]-v[i])

return y

def con11(inp,y1,y2,y3): # Z value

z=convo(inp,y1)

return z

def con12(inp,y1,y2,y3): # W value

z=convo(inp,y1)

w=convo(z,y2)

return w

def con13(inp,y1,y2,y3): # V Value

z=convo(inp,y1)

w=convo(z,y2)

v=convo(z,y3)

return v

def con2(inp,y1,y2,y3): # Convolution using Approach 2

for i in range(len(y2),len(y3)):

[Link](0)

r1=[0]*len(y3)

for i in range(len(y3)):

r1[i]+=y2[i]-y3[i]

r1=r1[:7]

h=convo(y1,r1)

y=convo(inp,h)

return y
def coon2(inp,y1,y2,y3):

for i in range(len(y2),len(y3)):

[Link](0)

r1=[0]*len(y3)

for i in range(len(y3)):

r1[i]+=y2[i]-y3[i]

r1=r1[:7]

h=convo(y1,r1)

y=convo(inp,h)

return h

def shift(y,a): # Shifting

h=[0]*len(y)

for i in range(len(y)):

s=i+a

if 0<=s<len(y):

h[s]=y[i]

return h

xn=[2]*7 #Plotting All Values

h1,h2,h3=sys()

out1=con1(xn,h1,h2,h3)

he=coon2(xn,h1,h2,h3)

print("H[n]")

print(he)

h1,h2,h3=sys()

out2=con2(xn,h1,h2,h3)

print("Output")

print(out1)

print(out2)
z=con11(xn,h1,h2,h3)

w=con12(xn,h1,h2,h3)

v=con13(xn,h1,h2,h3)

v=v[:17]

print("Z[n]")

print(z)

print("W[n]")

print(w)

print("V[n]")

print(v)

x=[Link](-5,12)

[Link](x,out1)

[Link]("n")

[Link]("y[n]")

[Link]("Output using Approach 1 ")

[Link]()

[Link]()

[Link](x,out2)

[Link]("n")

[Link]("y[n]")

[Link]("Output using Approach 2 ")

[Link]()

[Link]()

x1=[Link](-2,21)

x2=[Link](-5,6)

[Link](x2,z)

[Link]("n")

[Link]("z[n]")

[Link]("Z[n] Intermediate")

[Link]()

[Link]()
x2=[Link](-5,18)

[Link](x2,w)

[Link]("n")

[Link]("w[n]")

[Link]("W[n] Intermediate")

[Link]()

[Link]()

[Link](x,v)

[Link]("n")

[Link]("v[n]")

[Link]("V[n] Intermediate")

[Link]()

[Link]()

[Link](x1,he)

[Link]("n")

[Link]("H[n]")

[Link]("Total system response H[n] in Approach 2 ")

[Link]()

[Link]()

x3=[Link](-2,3)

y1=[-2,-1,0,1,2]

[Link](x3,y1)

[Link]("n")

[Link]("h1[n]")

[Link]("H1[n]")

[Link]()

[Link]()

x4=[Link](0,3)

y2=[0.5,0.5,0.5]

[Link](x4,y2)

[Link]("n")
[Link]("h2[n]")

[Link]("H2[n]")

[Link]()

[Link]()

x5=[Link](3,7)

y3=[0.25,0.25,0.25,0.25]

[Link](x5,y3)

[Link]("n")

[Link]("h3[n]")

[Link]("H3[n]")

[Link]()

[Link]()

x6=[Link](-3,4)

y4=[2,2,2,2,2,2,2]

[Link](x6,y4)

[Link]("n")

[Link]("x[n]")

[Link]("X[n]")

[Link]()

[Link]()

Common questions

Powered by AI

The arrays y1, y2, and y3 represent discrete signals or impulse responses that are used in the convolution operations to simulate effects on the input signal. y1 provides a range for basic signal extension, y2 represents an impulse response with a uniform value of 0.5 for three points, and y3 adds another layer of convolution with longer but smaller amplitude values (0.25). These arrays determine how the input signal 'inp' is filtered or transformed through successive convolution processes, highlighting their role as system components in digital signal processing .

Improvements to enhance the system could include optimizing convolution implementation through the Fast Fourier Transform (FFT) for faster computations, especially for large signals. Introducing adaptive filtering techniques could improve real-time processing and handling of non-stationary signals. Parallel computing implementations might enhance efficiency by concurrently computing convolutions. Another improvement is implementing error checking and robustness features to handle signal anomalies or noise more effectively .

Using console outputs for intermediate results like Z[n], W[n], and V[n] is effective for quick, immediate feedback during debugging or initial exploration of signal processing algorithms. It allows programmers to verify computations without graphical output dependencies, ensuring clarity in numerical values across computational steps. However, it lacks the intuitive understanding that visual plots provide, which are crucial for confirming conceptual results and trends in the data .

Zero-padding is necessary in the convolution functions to ensure that arrays involved in convolution are of the appropriate lengths to allow full overlap when performing the operation. This increases the convolved signal's length to 'len(x) + len(h) - 1'. In the code, zero-padding is explicitly added to the array 'h' in the 'convo' function, extending its length to match that of the expected output .

In Approach 1, convolution is performed in a sequence where the input signal 'inp' is first convolved with 'y1' to get 'z', and then 'z' is convolved with 'y2' and 'y3' separately to get 'w' and 'v', respectively. The output 'y' is obtained by subtracting 'v' from 'w' . In Approach 2, a combined filter 'r1' is first formed by subtracting 'y3' from 'y2', and then 'y1' is convolved with 'r1' to form an equivalent system response 'h'. The input is then convolved with this combined response 'h'. The main difference is that Approach 1 uses serial convolution operations, while Approach 2 computes a combined filter and applies a single convolution .

Challenges in real-time hardware implementation include processing delays due to slower hardware computation limits and resource constraints, such as memory and processing power. Signal aliasing could also pose issues if sampling rates are not adequately adjusted. To address these, optimizing algorithms for hardware efficiency (using methods like FFT), implementing parallel processing on multi-core architectures, and ensuring proper anti-aliasing filters are crucial. Additionally, developing adaptive systems that self-tune to changes in signal parameters could enhance robustness against real-time variability .

Approach 1 involves direct sequential convolution of the input with each signal component (y1, y2, y3), then a differential operation, producing an output dependent on hierarchical operations. Approach 2 combines impulse responses (y2 and y3) beforehand, convolves this combination (r1) with y1, before convolving with input. While theoretical results should align under ideal conditions, practical discrepancies may arise from accumulated errors or zero-padding effects across multiple operations. Hence, resulting outputs may exhibit amplitude and timing variations, reflecting systematic propagation of difference aggregation in Approach 2 compared to separate channel processing in Approach 1 .

The 'shift' function is designed to shift elements of the input array by a specified amount 'a'. It creates a new list 'h' initialized with zeros, of the same length as the input. It iterates over the input array, and for each element, it recalculates its position 's' by adding 'a' to its index. If 's' remains within valid indices, the element is placed in 'h' at position 's'. This function is used in convolution operations to account for temporal shifts in signals .

The plotting functions are used to visualize the intermediate and final results of the convolution processes. They provide a graphical representation of the signals and systems at each step, such as intermediate results like 'Z[n]', 'W[n]', 'V[n]', and complete outputs for different approaches. This visualization helps in understanding the effects of different operations, debugging processes, and confirming theoretical predictions about system behavior .

The 'convo' function handles convolution by first determining the total length of the convolution result as 'len(x) + len(h) - 1'. It initializes the output array with zeros of this length. It then appends zeros to 'h' to extend its length to the intended result length. For each element in the input signal 'x', it shifts 'h' accordingly and multiplies each resulting element by 'x[i]', accumulating these products into the result array 'y'. This method reflects the definition of discrete convolution as a sum of products of shifted impulse responses .

You might also like