脚本中的函数定义必须出现在文件的结尾。 请将 "ExtractPowerSpectralFeature" 函数定义后面的所有语句都移到第一个局部函数定义前面。
时间: 2023-06-17 07:03:02 浏览: 173
好的,下面是修改后的代码:
```python
import numpy as np
import librosa
def ExtractPowerSpectralFeature(audio_path):
y, sr = librosa.load(audio_path)
# STFT, short-term Fourier transform
n_fft = 2048
hop_length = 512
S = np.abs(librosa.stft(y, n_fft=n_fft, hop_length=hop_length))
# power spectrogram
power = S**2
# spectral centroid
spectral_centroids = librosa.feature.spectral_centroid(S=S)
# spectral bandwidth
spectral_bandwidths = librosa.feature.spectral_bandwidth(S=S)
# spectral contrast
spectral_contrasts = librosa.feature.spectral_contrast(S=S)
# spectral rolloff
spectral_rolloff = librosa.feature.spectral_rolloff(S=S)
# mel-frequency cepstral coefficients (MFCCs)
n_mfcc = 20
mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=n_mfcc)
# concatenate all features
features = np.concatenate((power, spectral_centroids, spectral_bandwidths, spectral_contrasts, spectral_rolloff, mfccs), axis=0)
return features
def ExtractFeatures(audio_path):
features = ExtractPowerSpectralFeature(audio_path)
return features
```
请注意,修改后的代码中,函数 `ExtractPowerSpectralFeature` 的定义出现在了文件的开头,并且在 `ExtractFeatures` 函数中被调用。这样做是因为在 `ExtractFeatures` 函数中调用 `ExtractPowerSpectralFeature` 函数,所以 `ExtractPowerSpectralFeature` 函数的定义需要在 `ExtractFeatures` 函数之前。
阅读全文