Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

How can I scale (x-axes) and shift data within array in Python?

I have an array of data that represents some signal f(x). If there is a way to perform operations which gives me in result an array of f(ax + b) by using only first array?

For "+ b" shifting part I use numpy.insert to insert array of zeros to shift signal left or right, but can't figure how to do f(ax). Please keep in mind that I do not want to a*f(x) and simple multiplication of array by constant is not an option.

Edit: Unfortunately I have no access to function that generated first array, I think that resampling functions are the ones that will solve rescalling issue.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Depending on the size of the array there are several solutions, the simplest is to access the array f as f[a*x+b] and checking if that is a valid index. Here is a code that creates the shifted array:

import numpy as np
def scale_shift(f, a , b):
    i = np.arange(len(f))*a+b
    y = f[i[(0<=i) & (i<len(f))]]
    return y

n = 10
f = np.random.rand(n)
print(scale_shift(f,2,1))

Note that the length of the new array will depend on the shift. You can use % if you want to wrap around the boundaries


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...