I am learning Numpy as a substitute to Matlab. I am having problem mapping Matlab function to Numpy. Program is to add two signals using Matlab
Matlab version :
function [y.d = sigadd(xl,nl,x2,n2)
% implements y(n) = xi(n)+x2(n)
% [y,nl - sigadd(xi,nl,x2,n2)
X
% xi = first sequence over nl
% x2 - second sequence over n2 (n2 can be different from nl)
%
n = min(min(n1) ,min(n2)) :max(max(nl) ,max(n2)) ; X duration of y(n)
yl - zeros(l,length(n)); y2 = yl;
yl(find((n>=min(nl))&(n<cmar(nl))-l))lxl;
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2;
y = yl+y2;
I have tried the following in Python :
def SigAdd(x1,n1,x2,n2):
n_l = min(min(n1),min(n2))
n_h= max(max(n1),max(n2))
n = arange(n_l,n_h+1)
y1 = zeros([1,len(n)],int)
y2 = y1
y1 = (argwhere((n>=min(n1))&(n<=max(n1)))==1)
y1 = x1
y2 = (argwhere((n>=min(n2))&(n<=max(n2)))==1)
y2 = x2
y = y1 + y2
return y,n
Expected Results:
Example1: Arrays of unequal length
x1 = array([1,1,1,1,1])
x2 = array([1,1,1,1])
n1 = array([1,2,3,4,5])
n2 = array([1,2,3,4])
y,n = SigAdd(x1,n1,x2,n2)
>> y
array[2,2,2,2,1]
>>n
array[1,2,3,4,5]
Example2:Arrays of equal length
x1 = array([1,1,1])
x2 = array([1,1,1])
n1 = array([1,2,3])
n2 = array([3,4,5])
y,n = SigAdd(x1,n1,x2,n2)
>> y
array[1,1,2,1,1]
>>n
array[1,2,3,4,5]
It works fine if both the arrays are of equal length, but won't work for unequal arrays. I know the problem is i am overriding the y1 (which i initially created with zeros) with value of x1 causing problem. I have used argwhere command in Numpy which is equivalent to Find command of Matlab, but if I use it like Matlab program shown above, it shows error that value can't be assigned to callable function.
Basically in Matlab program, unequal arrays are filled with zero's. Matlab version handles condition even if two signals are of equal length but are at different positions. I want to use Python instead of Matlab but these conversion problems are causing pain.
Some Modifications : (But is not working, Indexing Error : Out of bound coming)
def SigAdd(x1,n1,x2,n2):
n_l = min(min(n1),min(n2))
n_h= max(max(n1),max(n2))
n = arange(n_l,n_h+1)
y1 = zeros([1,len(n)],int)
y2 = y1
y11 = argwhere((n>=min(n1))&(n<=max(n1)))
q = 0
for w in y11:
y1[w]= x1[q]
q = q + 1
y22 = argwhere((n>=min(n2))&(n<=max(n2)))
q = 0
for w in y22:
y2[w]= x2[q]
q = q + 1
y = y1 + y2
return y
SigAdd().