2

I want to get an average amplitude of the sound file for every second. For example the average amplitude of 0-1 sec,1-2 sec, and so on. I tried reducing the sample rate to 1 but the value drops to 0 in that case.

import numpy as np 
import matplotlib.pyplot as plt 
from glob import glob
import librosa as lr

y, sr = lr.load(file,sr=10)
print(y)
print(len(y))
time = np.arange(0,len(y))/sr
print(len(time))
print(time,y)

print(y)
fig, ax = plt.subplots()
ax.plot(time,y)
ax.set(xlabel='Time(s)',ylabel='sound amplitude')
plt.show()

Note: I'm trying to get the amplitude value in dB. Really new with Librosa, would appreciate any help:)

Lakshya Kumar
  • 65
  • 1
  • 8

1 Answers1

2

This will do average amplitude, which is usually going to be 0.

y, sr = lr.load(file)
second = []
for s in range(0,len(y),sr):
    second.append( y[s:s+sr].mean() )

This will do average abs amplitude.

y, sr = lr.load(file)
second = []
for s in range(0,len(y),sr):
    second.append( np.abs(y[s:s+sr]).mean())
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30