【发布时间】:2016-01-18 11:19:02
【问题描述】:
我在 Python 中对音频频谱图进行了 PCA,并面临以下问题:我有一个矩阵,其中每一行都包含扁平化的歌曲特征。应用 PCA 后,我很清楚,尺寸减小了。但是我在常规数据集中找不到那些维度数据。
import sys
import glob
from scipy.io.wavfile import read
from scipy import signal
from scipy.fftpack import fft
import numpy as np
import matplotlib.pyplot as plt
import pylab
# Read file to get samplerate and numpy array containing the signal
files = glob.glob('../some/*.wav')
song_list = []
for wav in files:
(fs, x) = read(wav)
channels = [
np.array(x[:, 0]),
np.array(x[:, 1])
]
# Combine channels to make a mono signal out of stereo
channel = np.mean(channels, axis=0)
channel = channel[0:1024,]
# Generate spectrogram
## Freqs is the same with different songs, t differs slightly
Pxx, freqs, t, plot = pylab.specgram(
channel,
NFFT=128,
Fs=44100,
detrend=pylab.detrend_none,
window=pylab.window_hanning,
noverlap=int(128 * 0.5))
# Magnitude Spectrum to use
Pxx = Pxx[0:2]
X_flat = Pxx.flatten()
song_list.append(X_flat)
song_matrix = np.vstack(song_list)
如果我现在将 PCA 应用于 song_matrix...
import matplotlib
from matplotlib.mlab import PCA
from sklearn import decomposition
#test = matplotlib.mlab.PCA(song_matrix.T)
pca = decomposition.PCA(n_components=2)
song_matrix_pca = pca.fit_transform(song_matrix.T)
pca.components_ #These components should be most helpful to discriminate between the songs due to their high variance
pca.components_
...最后两个组件如下: Final components - two dimensions from 15 wav-files 问题是,我无法在所有维度的原始数据集中找到这两个向量我做错了什么还是我误解了整个事情?
【问题讨论】:
标签: python audio machine-learning pca principal-components