import numpy as np import matplotlib.pyplot as plt from scipy.signal import get_window 生成信号 fs = 10000 t = np.arange(0, 1, 1/fs) x = np.sin(2np.pi1000*t) 窗函数 win_dict = {'rect': 'boxcar', 'hanning': 'hann', 'hamming': 'hamming', 'blackman': 'blackman', 'kaiser': 'kaiser'} win_len = 256 beta = 5 频谱分析 for win_name, win_func in win_dict.items(): win = get_window(win_func, win_len) xw = x[:win_len] * win Xw = np.fft.fft(xw, win_len) freq = np.fft.fftfreq(win_len, 1/fs) Xw_db = 20*np.log10(abs(Xw)) Xw_db -= Xw_db.max() plt.plot(freq, Xw_db, label=win_name) 图像显示 plt.xlim(0, 5000) plt.ylim(-60, 0) plt.xlabel('Frequency (Hz)') plt.ylabel('Magnitude (dB)') plt.legend() plt.show() 将上面这串代码,转换成MATLAB的代码,要求最终结果不变
时间: 2024-01-27 13:03:59 浏览: 463
配置Python+numpy+scipy+matplotlib环境.pdf
fs = 10000;
t = 0 : 1/fs : 1-1/fs;
x = sin(2*pi*1000*t);
win_dict = containers.Map({'rect', 'hanning', 'hamming', 'blackman', 'kaiser'}, {'boxcar', 'hann', 'hamming', 'blackman', 'kaiser'});
win_len = 256;
beta = 5;
for win_name = keys(win_dict)
win_func = win_dict(win_name);
win = get_window(win_func, win_len);
xw = x(1:win_len) .* win';
Xw = fft(xw, win_len);
freq = fftshift((-win_len/2:win_len/2-1)*(fs/win_len));
Xw_db = 20*log10(abs(Xw));
Xw_db = Xw_db - max(Xw_db);
plot(freq, Xw_db, 'DisplayName', win_name);
hold on;
end
xlim([0, 5000]);
ylim([-60, 0]);
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
legend('Location', 'southwest');
grid on;
hold off;
阅读全文