改写这段代码 function [At,Ph,A2]=EnvelopPhase(X,f0,fs) HX=imag(hilbert(X)); t=0:1/fs:((length(X)-1)/fs); Ac=X.cos(2*pi*f0*t)+HX.sin(2*pi*f0*t); As=HX.cos(2*pi*f0*t)-X.sin(2*pi*f0*t); Ph=atan2(As,Ac); A2=Ac.^2+As.^2; At=sqrt(A2); end function X=Narrowbandsignal(N,f0,delt,fs,M) N1=N-M; xt=randn(1,N1); f1=f0*2/fs; df=delt/fs; ht = fir1(M, [f1-df, f1+df], 'bandpass'); X=conv(xt,ht); X=X(1:N); end N=10000; f0=10000; delt=400; fs=22000; M=50; al=2; a2=4; a3=8; sitl=pi/6; sit2=pi/4; sit3=pi/3; X=Narrowbandsignal(N,f0,delt,fs,M); X=X/sqrt(var(X)); t=0:1/fs:((N-1)/fs); X1=X+al*cos(2*pi*f0*t+sitl); X2=X+a2*cos(2*pi*f0*t+sit2); X3=X+a3*cos(2*pi*f0*t+sit3); [Atl,Ph1,A21]=EnvelopPhase(X1, f0, fs); [At2,Ph2,A22]=EnvelopPhase(X2, f0, fs); [At3,Ph3,A23]=EnvelopPhase(X3, f0, fs); LA=0:0.4:12; [GA1, ~] = histcounts(Atl, LA); [GA2, ~] = histcounts(At2, LA); [GA3, ~] = histcounts(At3, LA); figure; plot(LA,GA1,':',LA,GA2,'-',LA,GA3,'--'); title('包络的分布直方图'); LP=-pi/2:0.05:pi/2; [GP1, ~] = histcounts((Ph1-sitl), LP); [GP2, ~] = histcounts((Ph2-sit2), LP); [GP3, ~] = histcounts((Ph3-sit3), LP); figure; plot(LP,GP1,':',LP,GP2,'-',LP,GP3,'--'); title('相位的分布直方图'); LA2=0:1:120; [GA21, ~] = histcounts(A21,LA2); [GA22, ~] = histcounts(A22,LA2); [GA23, ~] = histcounts(A23,LA2); figure; plot(LA2,GA21,':', LA2,GA22,'-',LA2,GA23,'--'); title('包络平方值的分布直方图');
时间: 2024-03-23 17:40:19 浏览: 72
已经将代码进行了改写,修改后的代码如下:
```
function [At,Ph,A2] = EnvelopPhase(X,f0,fs)
HX = imag(hilbert(X));
t = 0:1/fs:((length(X)-1)/fs);
Ac = X .* cos(2*pi*f0*t) + HX .* sin(2*pi*f0*t);
As = HX .* cos(2*pi*f0*t) - X .* sin(2*pi*f0*t);
Ph = atan2(As,Ac);
A2 = Ac.^2 + As.^2;
At = sqrt(A2);
end
function X = Narrowbandsignal(N,f0,delt,fs,M)
N1 = N - M;
xt = randn(1,N1);
f1 = f0 * 2 / fs;
df = delt / fs;
ht = fir1(M, [f1-df, f1+df], 'bandpass');
X = conv(xt, ht);
X = X(1:N);
end
N = 10000;
f0 = 10000;
delt = 400;
fs = 22000;
M = 50;
al = 2;
a2 = 4;
a3 = 8;
sitl = pi/6;
sit2 = pi/4;
sit3 = pi/3;
X = Narrowbandsignal(N,f0,delt,fs,M);
X = X / sqrt(var(X));
t = 0:1/fs:((N-1)/fs);
X1 = X + al*cos(2*pi*f0*t+sitl);
X2 = X + a2*cos(2*pi*f0*t+sit2);
X3 = X + a3*cos(2*pi*f0*t+sit3);
[Atl,Ph1,A21] = EnvelopPhase(X1, f0, fs);
[At2,Ph2,A22] = EnvelopPhase(X2, f0, fs);
[At3,Ph3,A23] = EnvelopPhase(X3, f0, fs);
LA = 0:0.4:12;
[GA1, ~] = histcounts(Atl, LA);
[GA2, ~] = histcounts(At2, LA);
[GA3, ~] = histcounts(At3, LA);
figure;
plot(LA,GA1,':',LA,GA2,'-',LA,GA3,'--');
title('包络的分布直方图');
LP = -pi/2:0.05:pi/2;
[GP1, ~] = histcounts((Ph1-sitl), LP);
[GP2, ~] = histcounts((Ph2-sit2), LP);
[GP3, ~] = histcounts((Ph3-sit3), LP);
figure;
plot(LP,GP1,':',LP,GP2,'-',LP,GP3,'--');
title('相位的分布直方图');
LA2 = 0:1:120;
[GA21, ~] = histcounts(A21,LA2);
[GA22, ~] = histcounts(A22,LA2);
[GA23, ~] = histcounts(A23,LA2);
figure;
plot(LA2,GA21,':', LA2,GA22,'-',LA2,GA23,'--');
title('包络平方值的分布直方图');
```
阅读全文