赞
踩
clear all;close all;clc; %清理工作区,关闭所有窗口,清空文本 Fs = 100; t = 0:1/Fs:5-1/Fs; x = sin(2*pi*5*t) + cos(2*pi*10*t-pi/2); % 信号 figure(1); subplot(311) plot(x) %计算信号的傅里叶变换,绘制单边幅度谱 y = abs(fft(x)); % y = fftshift(y); % 将零频分量移到频谱中心,即频谱关于纵轴对称显示 ly = length(y); y = y(1:ly/2+1); % 当N是奇数时,单边谱是长度为(N+1)/2的序列,当N是偶数时,单边谱是长度为(N/2)+1的序列 % 单边频谱只保留一半能量,为了表示真实的振幅,需要将直流和奈奎斯特频率外的其余分量乘2。 y(2:end-1) = 2*y(2:end-1); f = (0:ly/2)/ly*Fs; subplot(312) plot(f,y) title("one-Sided Magnitude Spectrum of x(t)") xlabel("Frequency (Hz)") ylabel("2 * |y|") grid subplot(313) plot(f,10*log10(y)) % 转为dB格式,压缩动态范围,使得增益、损耗等操作简单化 title("one-Sided Magnitude Spectrum of x(t)") xlabel("Frequency (Hz)") ylabel("Power (dB)") grid % 功率谱计算,表示信号在每个频率分量上的功率,计算:信号傅里叶变换幅值的平方/区间长度 figure(2) subplot(311) temp = y.^2/ly; plot(f,temp) title("one-Sided Power Spectrum of x(t)") xlabel("Frequency (Hz)") ylabel("Power") grid % 功率谱密度计算,单位频带内的信号功率,计算:信号傅里叶变换幅值的平方/(区间长度*采样率) subplot(312) temp = y.^2/(ly * Fs); plot(f,temp) title("one-Sided Power Spectrum of x(t)") xlabel("Frequency (Hz)") ylabel("Power density") grid % periodogram直接计算功率谱密度,可用于校验其他方式计算的功率谱密度正确性 subplot(313) temp = periodogram(x, [], ly, Fs); plot(f,temp) title("one-Sided Power Spectrum of x(t)") xlabel("Frequency (Hz)") ylabel("Power density") grid % 相位谱计算 figure(3) y = fft(x); y = y(1:ly/2+1); threshold = max(abs(y)) * 0.1; % 设置幅度阈值,仅查看主要频率成分,取最大幅值的10%作为阈值 significant_indices = find(abs(y) > threshold); theta = angle(y); stem(f(significant_indices), theta(significant_indices)) title("Phase Spectrum of x(t)") xlabel("Frequency (Hz)") ylabel("Phase/\pi") grid
结果如下:
原时域信号、线性幅度谱和分贝幅度谱
功率谱、功率谱密度(两种计算方式,下图中2图是3图中峰值的两倍,因为2图功率谱密度计算前幅值乘2还原能量)
相位谱,5zh处为正弦波,其本身带有π/2的相位偏移,10hz处为余弦波,模拟信号添加了π/2的偏移。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。