Question: I am getting some kind of casting error when I try to get the array in the last few lines of code. The following lines

I am getting some kind of casting error when I try to get the array in the last few lines of code. The following lines of code imports: 
(1) numpy: To import core library for scientific computing 
(2) matplotlib: To import libraries for plotting 
(3) scipy: To manipulate multi-dimensional arrays 
(4) wavfile: To read in audio file 
(5) Audio: To output an audio player to the notebook 

In [1]:

 
import numpy as np 
import matplotlib.pyplot as plt 
%matplotlib inline 
from scipy.io import wavfile 
import scipy.signal as sig 
from scipy.fftpack import fft, ifft 
from IPython.display import Audio 
np.set_printoptions(precision=3, suppress=True) 
 
Code below imports the wav file from the directory and reads the file to output the vowel sound. 

In [2]:

Fs, sound = wavfile.read('MySound.wav')#my vowel sound 
Audio(sound, rate=Fs) 

Out[2]:

Code below verifies that my sound data is one dimensional 

In [3]:

sound.shape 

Out[3]:

(23280,) 
Following code reads the sound and plots a short portion of my periodic sound wave. 

In [23]:

plt.plot(sound[2200:2500]) 

Out[23]:

[] 
Following code counts the number of samples of my sound .wav file 

In [5]:

sound.size # no. of samples 

Out[5]:

23280 
Code below graphs the sound wave and it can be seen that the volume decreases gradually towards the end. 
The frequency variable is quantized to 8000/8192 = 0.98  1Hz per sample. 
The periodogram function (sig.periodogram) calculates an estimate of the signals spectrum. 
Most of the energy is released at about 2500 - 3000 range. 

In [31]:

 
x = sound[:8192] 
N = len(x) 
f, Pxx = sig.periodogram(x, fs=8000) 
plt.semilogy(f,Pxx); 
 
To find the fundamental frequency, we plot the sound wave in the range where highest energy is obtained. 

In [24]:

plt.plot(f[:2500],Pxx[:2500]) 
plt.title('Periodogram Estimate of Signal Energy'); 
 
Fundamental frequency is about 164 Hz 

In [26]:

 plt.plot(f[125:200],Pxx[125:200]); #fundamental frequency = 164 Hz 
 
It results in a length of roughly 49 samples/period 

In [32]:

8000/164 #length per period 

Out[32]:

48.78048780487805 

Error Starts here:

In [27]:

peaks = sig.find_peaks_cwt(x,np.arange(10,100)) 
peaks[:8], np.diff(peaks[:9]) 

Out[27]:

(array([], dtype=float64), array([], dtype=float64)) 

In [29]:

 
np.bincount(np.diff(peaks))[150:] 
--------------------------------------------------------------------------- TypeError Traceback (most recent call last)  in () ----> 1 np.bincount(np.diff(peaks))[150:] TypeError: Cannot cast array data from dtype('float64') to dtype('int64') according to the rule 'safe' 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!