给我一段matlab代码,要求用MUSIC算法求功率谱,估计出频率f1,f2
时间: 2024-02-15 08:03:37 浏览: 92
好的,请看下面的代码:
```matlab
clear all; clc;
% 信号参数设置
fs = 1000; % 采样频率
f1 = 50; % 正弦波频率1
f2 = 80; % 正弦波频率2
N = 1000; % 信号长度
n = 0:N-1;
A = [1, 0.5]; % 正弦波振幅
% 生成信号
s1 = A(1)*sin(2*pi*f1*n/fs);
s2 = A(2)*sin(2*pi*f2*n/fs);
s = s1 + s2;
% 添加白噪声
SNR = 10; % 信噪比
noise = randn(1,N);
Psignal = sum(s.^2)/N; % 信号功率
Pnoise = sum(noise.^2)/N; % 噪声功率
noise = noise/sqrt(Pnoise)*sqrt(Psignal/10^(SNR/10));
x = s + noise;
% MUSIC算法估计功率谱及频率
M = 4; % 阵元数
d = 0.5; % 阵元间距
theta = 0:0.5:180; % 角度扫描范围
theta = theta/180*pi;
Pmusic = zeros(1,length(theta));
for k = 1:length(theta)
a = exp(-1j*2*pi*d*sin(theta(k))*(0:M-1)');
Pmusic(k) = 1/(a'*(pinv(x*x')*a));
end
[val,idx] = findpeaks(abs(Pmusic)/max(abs(Pmusic)),theta/pi*180);
f_est = sort(sin(idx/180*pi)*fs/(2*d));
% 画图
figure;
plot(theta/pi*180,10*log10(abs(Pmusic)/max(abs(Pmusic))));
hold on;
plot(idx,val,'r*');
xlabel('Angle (degree)');
ylabel('Power spectrum (dB)');
title('MUSIC algorithm power spectrum');
legend('Power spectrum','Frequency estimation');
```
这段代码实现了生成两个正弦波信号,加入一定信噪比的噪声,然后利用MUSIC算法估计信号的功率谱及频率。通过寻找功率谱中的峰值,并利用其相应的角度估计出频率f1,f2。
阅读全文