I am trying to define a function in Python in order to use it later, and there is a problem in using if statement inside of the function. The goal is define the following function for every real argument and, as for special points (where the value is defned by a limit), define it from the condition. So the code is
import numpy as np;
#%% Impulse response of a band pass filter (BPF)
t_0_BPF = 10;
A_fc = 1;
def impulse_responce_BPF(t_var):
if t_var == t_0_BPF:
return 2*A_fc/np.pi
else:
return 2*A_fc/np.pi * np.sin (2*np.pi*20/2*(t_var-t_0_BPF) )/(t_var-t_0_BPF) *np.cos(2*np.pi*45*(t_var-t_0_BPF))
I have to add an "if" condition in order to avoid to have "nan" values when t_var = t_0_BPF (sinc funciton).
So when I am trying to use the function when the input is an array, there is a problem.
t = np.zeros(10)
impulse_responce_BPF(t)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[1], line 14
11 return 2*A_fc/np.pi * np.sin (2*np.pi*20/2*(t_var-t_0_BPF) )/(t_var-t_0_BPF) *np.cos(2*np.pi*45*(t_var-t_0_BPF))
13 t = np.arange(10);
---> 14 impulse_responce_BPF(t)
Cell In[1], line 8, in impulse_responce_BPF(t_var)
6 def impulse_responce_BPF(t_var):
7 # it is proportional to sin(2 pi deltaF_BW/2 * t) / t *cos(2 pi * F_center)
----> 8 if t_var == t_0_BPF:
9 return 2*A_fc/np.pi
10 else:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I understand that there is a problem in interpreting the logic condition "t_var == t_0_BPF:" which is an array. Anyway, I am interested in using the function in this vectorized way. Is there a way to not having this error? Or to define the function differently.