|
我修改基波程序如下,谐波部分LZ自已再加上去:
clc;
clear;
close all;
format short g;
f0=50.5;
fs=50*256;
N=2048;%2048/256=8
n=[0:1:N-1];
xb=[220,0.5,25,0.4,6,0.3,4,0.2,2];
Q=[pi/3,pi/3,pi/3,pi/3,pi/3,pi/3,pi/3,pi/3,pi/3];
s=zeros(1,N);
for i=1:9
s=s+xb(i)*cos(2*pi*f0*i*n./fs+Q(i));
end
%figure (1);
%plot(s);
w=0.54-0.46*cos(2*pi*n./N);
r=s.*w;
v=fft(r,N);
u=abs(v);
%为了求出谐波,那么必须找出基波,在35-65中频谱最大值所对应的就是离基波频率最近的那个点
y=0;
n1=fix(35/(fs/N))+1; n2=fix(65/(fs/N))+1;
[y,num]=max(u(n1:n2));
num=num+n1-1;
%★★★★★★谱线最大值已经找到,下面是要确定k1和k2还有y1和y2★★★★★★
k1=zeros(1,63);
k2=zeros(1,63);
if u(num+1)<u(num-1)
num=num-1;
end
k1(1)=num; k2(1)=num+1;
y1=u(num); y2=u(num+1);
%%★★★★★★k1和k2还有y1和y2确定完毕★★★★★★
%%★★★★★★确定αβ,在这里用a和b表示,确定基波频率★★★★★★
A=zeros(1,63);
ff=zeros(1,63);
Ph=zeros(1,63);
b=(y2-y1)/(y2+y1);
a=1.21874943*b+0.13349531*b^3+0.05301420*b^5+0.03656014*b^7;
A(1)=(y1+y2)*(2.26557103+1.22719978*a^2+0.37607775*a^4+0.09767389*a^6)/N; %基波幅值
ff(1)=(k1(1)-1+a+0.5)*fs/N; %基波频率
Ph(1)=phase(v(k1(1)))-pi*(a+0.5); %基波相位
%%★★★★★★到这里,基波频率,基波幅值,基波相位确定完毕★★★★★★
fprintf('%5.6f %5.6f %5.6f\n',A(1),ff(1),Ph(1)); |
|