pyACA: Documentation 0.3.1
Source Code for Audio Content Analysis
Loading...
Searching...
No Matches
Functions | Variables
pyACA.computeSpectrogram Namespace Reference

Functions

 computeSpectrogram (x, f_s, afWindow=None, iBlockLength=4096, iHopLength=2048, bNormalize=True, bMagnitude=True)
 computes a spectrogram from the audio data
 
 computeSpectrogramCl (cPath)
 main
 

Variables

 parser = argparse.ArgumentParser(description='Compute key of wav file')
 
 metavar
 
 required
 
 help
 
 args = parser.parse_args()
 
 cPath = args.infile
 

Function Documentation

◆ computeSpectrogram()

computeSpectrogram ( x,
f_s,
afWindow = None,
iBlockLength = 4096,
iHopLength = 2048,
bNormalize = True,
bMagnitude = True )

computes a spectrogram from the audio data

Parameters
xarray with floating point audio data (dimension samples x channels)
f_ssample rate of audio data
afWindowFFT window of length iBlockLength (default: hann), can be [] empty
iBlockLengthinternal block length (default: 4096 samples)
iHopLengthinternal hop length (default: 2048 samples)
bNormalizenormalize input audio file before fft computation (default: True)
bMagnitudereturn magnitude instead of complex spectrum (default: True)
Returns
X: spectrum
f: frequencies of bins
t: time stamps

Definition at line 23 of file computeSpectrogram.py.

23def computeSpectrogram(x, f_s, afWindow=None, iBlockLength=4096, iHopLength=2048, bNormalize=True, bMagnitude=True):
24
25 iBlockLength = np.int_(iBlockLength)
26 iHopLength = np.int_(iHopLength)
27
28 # Pre-process: down-mix, normalize
29 x = ToolPreprocAudio(x, bNormalize)
30
31 if afWindow is None:
32 # Compute window function for FFT
33 afWindow = ToolComputeHann(iBlockLength)
34
35 assert(afWindow.shape[0] == iBlockLength), "parameter error: invalid window dimension"
36
37 # block audio data
38 x_b, t = ToolBlockAudio(x, iBlockLength, iHopLength, f_s)
39
40 # allocate memory
41 iSpecDim = np.int_([(x_b.shape[1] / 2 + 1), x_b.shape[0]])
42 X = np.zeros(iSpecDim)
43 if not bMagnitude:
44 X = X.astype(complex)
45
46 norm = 2 / x_b.shape[1]
47
48 for n in range(0, x_b.shape[0]):
49 # windowed fft
50 tmp = np.fft.fft(x_b[n, :] * afWindow) * norm
51
52 # remove redundant spectrum parts
53 if bMagnitude:
54 X[:, n] = abs(tmp[range(iSpecDim[0])])
55 else:
56 X[:, n] = tmp[range(iSpecDim[0])]
57
58 # let's be pedantic about normalization
59 X[[0, iSpecDim[0]-1], :] = X[[0, iSpecDim[0]-1], :] / np.sqrt(2)
60
61 f = np.arange(0, iSpecDim[0]) * f_s / iBlockLength
62
63 return X, f, t
64

◆ computeSpectrogramCl()

computeSpectrogramCl ( cPath)

main

Definition at line 67 of file computeSpectrogram.py.

67def computeSpectrogramCl(cPath):
68 from pyACA.ToolReadAudio import ToolReadAudio
69
70 # read audio file
71 [f_s, x] = ToolReadAudio(cPath)
72
73 # for debugging
74 iBlockLength = 4096
75 iHopLength = 2048
76
77 # compute feature
78 [X, f, t] = computeSpectrogram(x, f_s, None, iBlockLength, iHopLength)
79
80 return X, f, t
81
82

Variable Documentation

◆ args

args = parser.parse_args()

Definition at line 92 of file computeSpectrogram.py.

◆ cPath

str cPath = args.infile

Definition at line 93 of file computeSpectrogram.py.

◆ help

help

Definition at line 89 of file computeSpectrogram.py.

◆ metavar

metavar

Definition at line 88 of file computeSpectrogram.py.

◆ parser

parser = argparse.ArgumentParser(description='Compute key of wav file')

Definition at line 87 of file computeSpectrogram.py.

◆ required

required

Definition at line 88 of file computeSpectrogram.py.