% NC, 2017-08-26.
clear;
clc;
close all;
%% Basic parameters
fs = 400e6; % sampling rate: 400MHz
f = 20e6; % basic signal frequency: 20MHz
f_ref = 20e6+10 ; % reference signal frequency
lambda = 780e-9; % wavelength:780nm
v_max = 1.53; % the maximum velocity: 1.53m/s
N = 1000; % the cutoff parameter for filtered signal
%% Reference signal
t0 = (1/fs):(1/fs):0.01;
s_ref = cos(2*pi*f_ref*t0);
s_sin = sin(2*pi*f*t0);
s_cos = cos(2*pi*f*t0);
%% Measurement signal s1
a1 = 1000;
t1_1 = (1/fs):(1/fs):0.002;
t1_2 = (1/fs):(1/fs):0.00153;
t1_3 = (1/fs):(1/fs):0.00294;
t1_4 = (1/fs):(1/fs):0.00153;
t1_5 = (1/fs):(1/fs):0.002;
d1_1 = t1_1*0;
d1_2 = d1_1(length(d1_1)) + 0.5*a1*t1_2.*t1_2;
d1_3 = d1_2(length(d1_2)) + t1_3*1.53;
d1_4 = d1_3(length(d1_3)) + t1_4.*1.53 - 0.5*a1*t1_4.*t1_4;
d1_5 = d1_4(length(d1_4)) + t1_5*0;
d1 = [d1_1, d1_2, d1_3, d1_4, d1_5];
figure;
plot(t0, d1);grid on;hold on;
xlabel('t (s)');ylabel('d (m)');
title('The theoretical displacement of s1');
% d1 = v_max*t0;
%d1 = 0.5*0.5*t0.*t0;
theta1 = (2*pi*4/lambda)*d1;
s1 = cos(2*pi*f_ref*t0 + theta1);
% figure;
% plot(t0, d1);grid on;hold on;
% xlabel('t (s)');ylabel('d1 (m)');
% title('The theoretical value of the calculation result from s1');
%% Measurement signal s2
% d2 = (v_max/2)*t0;
f2 = 1000;
d2 = 0.0002*sin(2*pi*f2*t0);
figure;
plot(t0, d2);grid on;hold on;
xlabel('t (s)');ylabel('d (m)');
title('The theoretical displacement of s2');
theta2 = (2*pi*4/lambda)*d2;
s2 = cos(2*pi*f_ref*t0 + theta2);
% figure;
% plot(t0, d2);grid on;hold on;
% xlabel('t (s)');ylabel('d2 (m)');
% title('The theoretical value of the calculation result from s2');
%% Measurement signal s3
t3_1 = (1/fs):(1/fs):0.002;
d3_1 = t3_1*0;
d3_2 = t3_1*0+lambda/4096/4;
d3_3 = t3_1*0+2*lambda/4096/4;
d3_4 = t3_1*0+3*lambda/4096/4;
d3_5 = t3_1*0+4*lambda/4096/4;
d3 = [d3_1, d3_2, d3_3, d3_4, d3_5];
figure;
plot(t0, d3);grid on;hold on;
xlabel('t (s)');ylabel('d (m)');
title('The theoretical displacement of s3');
theta3 = (2*pi*4/lambda)*d3;
s3 = cos(2*pi*f_ref*t0 + theta3);
% %% Add some noise
% level_n = 1.5e-3;
% s_ref = s_ref + (rand(1, length(t0))-0.5)*2*level_n;
% s1 = s1 + (rand(1, length(t0))-0.5)*2*level_n;
% s2 = s2 + (rand(1, length(t0))-0.5)*2*level_n;
% s3 = s3 + (rand(1, length(t0))-0.5)*2*level_n;
%% Test s1
phase0 = DFT_Cal(s_ref, s_sin, s_cos);
phase1 = DFT_Cal(s1, s_sin, s_cos);
phase_1 = phase1 - phase0;
%phase_1 = phase1;
d1_r = phase_1 / (2*pi*4/lambda);
figure;
plot(t0, d1_r);grid on;hold on;
xlabel('t (s)');ylabel('d1 (m)');
title('The calculated displacement s1');
%Error analysis
error1 = d1 - d1_r;
error1 = error1(N+1:length(t0)-N);
error1 = error1 - mean(error1);
error_4096_std = zeros(1, length(error1));
error_4096_std_up = error_4096_std + (2*pi/4096)/(2*pi*4/lambda);
error_4096_std_down = error_4096_std - (2*pi/4096)/(2*pi*4/lambda);
figure;
plot(t0(N+1:length(t0)-N), error1);grid on;hold on;
plot(t0(N+1:length(t0)-N), error_4096_std_up,'r');grid on;hold on;
plot(t0(N+1:length(t0)-N), error_4096_std_down,'r');grid on;hold on;
xlabel('t (s)');ylabel('error1 (m)');
title('The relative error of s1');
% frequency-domain analysis
% cas_calc(error1, fs);
%% Test s2
phase0 = DFT_Cal(s_ref, s_sin, s_cos);
phase2 = DFT_Cal(s2, s_sin, s_cos);
phase_2=phase2-phase0;
d2_r = phase_2 / (2*pi*4/lambda);
d2r=tsmovavg(d2_r, 's', 20) ;
figure;
plot(t0, d2_r);grid on;hold on;
axis([0 0.01 -0.0002 0.0002 ]);
xlabel('t (s)');ylabel('d2 (m)');
title('The calculated displacement s2');
% Error analysis
error2 = d2 - d2_r;
error2 = error2(N+1:length(t0)-N);
error2 = error2 - mean(error2);
%
% error_4096_std = zeros(1, length(error1));
% error_4096_std_up = error_4096_std + (2*pi/4096)/(2*pi*4/lambda);
% error_4096_std_down = error_4096_std - (2*pi/4096)/(2*pi*4/lambda);
figure;
plot(t0(N+1:length(t0)-N), error2);grid on;hold on;
plot(t0(N+1:length(t0)-N), error_4096_std_up,'r');grid on;hold on;
plot(t0(N+1:length(t0)-N), error_4096_std_down,'r');grid on;hold on;
xlabel('t (s)');ylabel('error2 (m)');
title('The relative error of s2');
%% Test s3
phase0 = DFT_Cal(s_ref, s_sin, s_cos);
phase3 = DFT_Cal(s3, s_sin, s_cos);
phase_3 =phase3- phase0;
d3_r = phase_3 / (2*pi*4/lambda);
%d3r=tsmovavg(d3_r, 's', 20) ;
figure;
plot(t0(N:length(t0)-N), d3_r(N:length(t0)-N));grid on;hold on;
%plot(t0(N:length(t0)-N), d3r(N:length(t0)-N));hold on;
xlabel('t (s)');ylabel('d3 (m)');
title('The calculated displacement s3');
% Error analysis
error3 = d2 - d2_r;
error3 = error3(N+1:length(t0)-N);
error3 = error3 - mean(error3);
%
% error_4096_std = zeros(1, length(error1));
% error_4096_std_up = error_4096_std + (2*pi/4096)/(2*pi*4/lambda);
% error_4096_std_down = error_4096_std - (2*pi/4096)/(2*pi*4/lambda);
figure;
plot(t0(N+1:length(t0)-N), error3);grid on;hold on;
plot(t0(N+1:length(t0)-N), error_4096_std_up,'r');grid on;hold on;
plot(t0(N+1:length(t0)-N), error_4096_std_down,'r');grid on;hold on;
xlabel('t (s)');ylabel('error3 (m)');
title('The relative error of s3');
% frequency-domain analysis
% cas_calc(error2, fs);
评论3