用matlab画相频曲线_MATLAB FFT函数画幅频和相频曲线

%%

%%

clc;

clear;

N=256;

f=50;

T=1/f;

fs=N*f;

Ts=1/N*T;

t=0:Ts:(2*N-1)*Ts;

y1=cos(2*pi*f*t)+0.05*cos(2*pi*41*f*t);

y2=cos(2*pi*f*t-1.98/4*pi)-0.4*cos(2*pi*41*f*t-1.98/4*pi);

% y1=cos(2*pi*10*f*t);

% y2=cos(2*pi*10*f*t);

figure;

stem(t,y1);

figure;

stem(t,y2);

detf= 1/720*pi;

% detf=0.02;

dett=detf/2/pi/f;

n=floor(dett/Ts);

t1=dett-n*Ts;

t2=(n+1)*Ts-dett;

y3=zeros(1,2*N);

y2p=cos(2*pi*f*t-1.98/4*pi-detf)-0.4*cos(2*pi*41*f*t-1.98/4*pi+1/180/100*pi);

% y2p=cos(2*pi*10*f*t+detf);

for ii=1:N

y3(ii)=(y2p(ii+n)*t2+y2p(ii+n+1)*t1)/(t1+t2);

end

for ii=N+1:2*N

y3(ii)=y3(ii-N);

end

figure;

subplot(2,1,1);

plot(t,y3);

subplot(2,1,2);

plot(t,y2);

figure;

plot(t,y2,'g',t,y3,'r');

z1=0;

z2=0;

for ii=N+1:2*N

z1=z1+y1(ii)*y2(ii);

z2=z2+y1(ii)*y3(ii);

end

err=(z2-z1)/z1;

disp(err);

% fs=f*N;

%

% fs=f*N;

% ts=1/fs;

% t=0:1/f/N:(2-1/N);

% y1=cos(2*pi*1*t);

%

% subplot(3,1,1);

% stem(t,y1);

%

% y2=cos(2*pi*1*t-pi/6);

%

% subplot(3,1,2);

% stem(t,y2);

%

% detf=1/6*pi;

% dett=detf/2/pi/f;

%

% n=floor(dett/ts);

% t1=dett-n*ts;

% t2=(n+1)*ts-dett;

%

%

% ybu=zeros(1,N*2);

% for ii=n+2:N*2

% ybu(ii)=(y1(ii-n-1)*t1+y1(ii-n)*t2)/(t1+t2);

% end

%

%

% ybuqian=zeros(1,n+1);

% for ii=1:n+1

% if ii-n-1<0

% yzuo=y1(2*N-(abs(ii-n-1)));

% %yyou=y1(2*N-abs(ii-n-1+1));

% else

% yzuo=y1(2*N)

% end

% % if ii-n<1

% yyou=y1(2*N-abs(ii-n));

% else

% yyou=y1(ii-n);

% end

% % % % ybuqian(ii)=(yzuo*t1+yyou*t2)/(t1+t2);

% end

%

% for ii=1:n+1

% ybu(ii)=ybuqian(ii);

% end

%

%

%

% subplot(3,1,3);

% stem(t,ybu);

%

% % nn=0:1:15;

% % y1=y1(1:16);

% % plot(nn,abs(fft(y1)));

figure;

kkk=0:2*N-1;

mm=kkk*fs/2/N/50;

ffty3=abs(fft(y3));

ffty3a=fft(y3);%y3,FFT后的复数

ffty2=abs(fft(y2));

ffty2a=fft(y2);%y2,FFT后的复数

y501=abs(ffty3a(3))/(2*N/2);

a501=atan(1*imag(ffty3a(3))/real(ffty3a(3)))/pi; %此处为1,网上有些写成了2

disp('y501==');

disp([y501,a501]);

y5041=abs(ffty3a(83))/(2*N/2); %例子中采样点数为2*N个,单个周期为N个,共采集了2个周期

a5041=atan(1*imag(ffty3a(83))/real(ffty3a(83)))/pi;

disp('y5041==');

disp([y5041,a5041]);

subplot(2,1,1)

plot(kkk*fs/2/N/50,abs(fft(y2))/N,'g');

subplot(2,1,2)

plot(kkk*fs/2/N/50,abs(fft(y3)/N),'r');

你可能感兴趣的:(用matlab画相频曲线)