ptrck
ptrck

Reputation: 79

Access Arrays by index

I want to access arrays like in the example code below, this is quite slow. Is it possible to create a vector from i ans f_s and access the arrays by that index?

def calc(self, length):
    for i in range(int(f_s*length*6)):
        t = i / f_s  
        self.data[i] = (numpy.multiply(sinTable512[int(t*f_carrier)%512], self.Signal[int(t*f_prn)%1023]))

I imagine the code to look something like this:

def calc(self,length):
    t = numpy.arange(0, f_s*length*6, 1/f_s)
    t_sin = t * f_carrier %512
    t_sig = t * f_prn % 1023
    self.data[i] = (numpy.multiply(sinTable512[t_sin], self.Signal[t_sig]))

Are there any other ways to do somethink like that? From what I remember the vector operations are a lot faster than for loops, at least in MatLab, is that the same for Python or is there another method to speed this operation up?

Upvotes: 3

Views: 113

Answers (1)

ptrck
ptrck

Reputation: 79

I found the answer by myself. The solution is to use numpy's take function. You can pass the array and a vector of indices to the function and it will return the desired arrays.

def calc(self,length):
    t = numpy.arange(0, f_s*length*6, 1/f_s)
    t_sin = t * f_carrier %512
    t_sig = t * f_prn % 1023
    self.data = (numpy.multiply(numpy.take(sinTable512, t_sin), numpy.take(self.Signal, t_sig)))

Upvotes: 1

Related Questions