简体   繁体   中英

How can I get a dataframe of frequency and time from a wav file in python?

i have been able to read a wave file using librosa, but was able to get a list of frequency and amplitudes which is missing the most important part "Time", i don't know how to go a raound

import librosa
from matplotlib import pyplot as plt
from librosa import display
import seaborn as sns

sns.set() # Use seaborn's default style to make attractive graphs
plt.rcParams['figure.dpi'] = 100 # Show nicely large images in this notebook

samples, sampling_rate = librosa.load('last.aac')
plt.figure(figsize=(13,4))
librosa.display.waveplot(y = samples, sr =  sampling_rate)

plt.xlabel("Time (Seconds)")
plt.ylabel("Amplitude")
plt.title("Time Domain Frequency")
plt.show()
print("sampling rate is :", sampling_rate)
print("Number of samples :", len(samples))

max1 = max(samples*10)
print("Maximum amplitude", round(max1, 2))

min1 = min(samples*10)
print("Minimum amplitude", round(min1, 2))

import pandas as pd
d=pd.DataFrame(samples,columns=["Amplitudes"], index=None)
d.head()


Amplitudes  frequencies
0.091039    2756.813965
-0.040613   2427.044189
-0.046945   2347.927002
-0.136473   2323.757080
0.050659    2317.414062

what i really want to extract is this below

Time Frequency Amplitude
0   0.00000 -57.72730
0  21.53320 -63.55554
0  43.06641 -85.05077
0  64.59961 -91.29989
0  86.13281 -83.86144
0 107.66602 -81.54240

If you have the sampling rate (in Hz), you can use that to create an index in seconds. This is an example using a 4-second audio file.

import librosa
import pandas as pd

y,sr = librosa.load('myfile.wav')

df = pd.DataFrame(y, columns=['Amplitude'])

df.index = [(1/sr)*i for i in range(len(df.index))]

print(df.head())
print(df.tail())

Output:

          Amplitude
0.000000  -0.004855
0.000045  -0.007008
0.000091  -0.005919
0.000136  -0.006276
0.000181  -0.005805

          Amplitude
3.999773   0.000892
3.999819   0.000765
3.999864   0.000543
3.999909   0.000428
3.999955   0.000216

The index is now in seconds.

Just to note, where you got the Frequencies column isn't shown in your example, and all the plotting stuff is not relevant to the issue, so you should trim some of that for posting questions on SO.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM