pyACA: Documentation 0.3.1
Source Code for Audio Content Analysis
Loading...
Searching...
No Matches
computeSpectrogram.py
Go to the documentation of this file.
1# -*- coding: utf-8 -*-
2
3import numpy as np
4
5from pyACA.ToolPreprocAudio import ToolPreprocAudio
6from pyACA.ToolComputeHann import ToolComputeHann
7from pyACA.ToolBlockAudio import ToolBlockAudio
8
9
10
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
65
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
83if __name__ == "__main__":
84 import argparse
85
86 # add command line args and parse them
87 parser = argparse.ArgumentParser(description='Compute key of wav file')
88 parser.add_argument('--infile', metavar='path', required=False,
89 help='path to input audio file')
90
91 # retrieve command line args
92 args = parser.parse_args()
93 cPath = args.infile
94
95 # only for debugging
96 if __debug__:
97 if not cPath:
98 cPath = "../ACA-Plots/audio/sax_example.wav"
99
100 # call the function
101 computeSpectrogramCl(cPath)