The Signal
class is a container based on the np.array
storing besides the samples as function of time also a sample frequency. The class has many usefull methods making the life of an acoustician a bit easier.
Furthermore, because its based on the np.array
, all operations you would normally perform on a np.array
can be done on a Signal
as well.
In [1]:
from acoustics import Signal
import numpy as np
%matplotlib inline
from IPython.display import Audio
import seaborn as sns # seaborn for prettier graphs
In [2]:
s = Signal(np.random.randn(44100), fs=11025)
Alternatively, one of the alternative constructors can be used
In [3]:
s = Signal.from_wav('../data/recording.wav')
In [4]:
s.fs
Out[4]:
The Signal
class can handle multiple signals. Currently we have only one channel.
In [5]:
s.channels
Out[5]:
The amount of samples in the signal can be retrieved in several ways. We can use the standard ways, len(s)
, or s.shape
but also the method s.samples
In [6]:
s.samples
Out[6]:
In [7]:
Audio(data=s, rate=s.fs)
Out[7]:
We can start with looking at the spectrum. A narrowband power spectrum can be calculated using s.power_spectrum()
In [8]:
s.power_spectrum()
Out[8]:
returning frequencies and powers. It's also possible to straightaway plot the power spectrum
In [9]:
fig = s.plot_power_spectrum()
There are other types of spectra available, like for example the phase spectrum. Another popular figure is the spectrogram
In [10]:
fig = s.spectrogram()
In [11]:
fig = s.plot_levels()
In [12]:
fig = s.plot_octaves()
Calculating or plotting 1/3-octaves is also possible.
In [13]:
fig = s.plot_third_octaves()
In [14]:
s.weigh('A')
Out[14]:
This can be seen better when plotting in octaves.
In [15]:
fig = s.weigh('A').plot_octaves()
In [16]:
s.weigh('A').leq()
Out[16]:
This value is a bit below the unweighted $L_{Z,eq}$.
In [17]:
s.leq()
Out[17]:
We can verify the calculation by using a different method. Let's calculate 1/3-octave values first, and then add the weighting. The A-weighting is
In [18]:
from acoustics.standards.iec_61672_1_2013 import WEIGHTING_VALUES
WEIGHTING_VALUES['A']
Out[18]:
and the A-weighted $L_{A,eq}$ determined using this method is
In [19]:
from acoustics.decibel import dbsum
dbsum( s.third_octaves()[1] + WEIGHTING_VALUES['A'] )
Out[19]:
In [20]:
s2 = Signal([s,s,s], s.fs)
as we can see
In [21]:
s2.channels
Out[21]:
Most methods still work when considering multichannel signals.
In [22]:
fig = s2.weigh('A').plot_third_octaves()