Stefan Denis
Stefan Denis

Reputation: 3

Scaling a vector between -1 and 1

I am currently trying to process voice signal.I have scaled a vector between -1 and 1. I have managed to do that but look at the graph of the vector i obtained. I need that signal to be centered in 0 in order to obtained the feature vector that will be given to a neural network. It is slightly below 0.

How can i scale this vector between -1 and 1 and keep the centering at 0 ?

Raw signal: enter image description here

Scaled signal: enter image description here

The code and and the normalization function:

samplerate, data = wavfile.read('avarii.wav')
times = np.arange(len(data))/float(samplerate)
print(times)
print(len(data))
print(samplerate)
# Make the plot
# You can tweak the figsize (width, height) in inches

#unit_vector = norm(data[:,1])


samples = data[:,1].tolist()

normalized_vector = norm(samples,-1,1)


#print(norm_vect) 
plt.figure(figsize=(30, 4))

plt.plot(times,normalized_vector)
# plt.fill_between(times, data[:,0], data[:,1], color='k') 
plt.xlim(times[0], times[-1])
plt.xlabel('time (s)')
plt.ylabel('amplitude')
# You can set the format by changing the extension
# like .pdf, .svg, .eps
plt.savefig('plot.png', dpi=100)
plt.show()
def norm(vector,a,b):



    #normalized_vector = []
    #standard_deviation = stdev(vector)

    max_value = max(vector)
    min_value = min(vector)
    #average = sum(vector)/len(vector)
    print(max_value)
    print(min_value)
    print(average)
    for value in vector:
        #value = value  - average
        norm_value = a + ((value - min_value)*(b-a))/(max_value - min_value)
        normalized_vector.append(norm_value)   

    # for value in vector:
    #     normalized_vector.append((value - average)/standard_deviation)


    # for value in vector:

    #     normalized_vector.append((value - min_value) / (max_value-min_value))

    return normalized_vector

To conclude i wwant to bound the values of these samples between [-1,1] and centered in 0. How can i do that ?

Upvotes: 0

Views: 917

Answers (1)

zvone
zvone

Reputation: 19352

First of all, you have numpy arrays. Don't convert them to list and then calculate values one by one. Use the power of numpy.

So, here's a copy of your solution, just with numpy:

samples = data[:,1]
min_value, max_value = samples.min(), samples.max()
normalized_vector = -1 + (samples - min_value) * 2 / (max_value - min_value)

Now, what you asked: The reason why zero went off is because the positive peak is greater than the negative peak, so the center is not in zero. You sould scale positive and negative equally:

samples = data[:,1]
max_peak = np.absolute(samples).max()
normalized_vector = samples / max_peak

That's it.

Upvotes: 1

Related Questions