Translating the values of one data set to another

Question:

I was wondering how i could ‘translate’ the values of one data set to another. Here is what I mean:

I have a data set of values that I would like to translate to midi pitches. This specific instrument in midi has a range of 33 through 128. I would like to translate values in my data set so they would fit in this range. For example: if I have a value that is the exact average of my data set, I want it to translate to 80.5 (average of this midi data set). Midi only accepts rounded numbers, but that’s beside the point.

I reckon I need a factor of some sorts. I’ve tried to come up with the correct one, but obviously it changes depending on the value and even depending on the data set. Meaning, if the minimum value of the data set is 33 and the maximum is 128, I could use factor 1. However, if the minimum is 33 and the maximum is 100, I can still use factor 1 with the value 33, but not with 100.

Any ideas?

Asked By: Fersy van der Zee

||

Answers:

This might help.

Example with regular lists:

X = [1,2,3,4,5,6,7,8,9,10]
minX = min(X)
maxX = max(X)
Y = []
for x in X:
    y = (((x-minX) * (128-33)) / (maxX-minX)) + 33
    Y.append(y)

print(Y)
Y = [33.0, 43.55555555555556, 54.111111111111114, 64.66666666666667, 75.22222222222223, 85.77777777777777, 96.33333333333334, 106.88888888888889, 117.44444444444444, 128.0]
Answered By: NirF
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.