add verification code of matlab
This commit is contained in:
		
							parent
							
								
									df1da34c44
								
							
						
					
					
						commit
						7a1c7f3523
					
				|  | @ -0,0 +1,6 @@ | ||||||
|  | %%% so means original, s_mean means mean value | ||||||
|  | function [s,s_mean] = MeanIntp(s,s_r1) | ||||||
|  | 
 | ||||||
|  |     s_mean = (s+s_r1)/2; | ||||||
|  | 
 | ||||||
|  | end | ||||||
|  | @ -0,0 +1,35 @@ | ||||||
|  | function out = MyIIR(a,b,x,YStartState,sel_double) | ||||||
|  | 
 | ||||||
|  | len = length(x); | ||||||
|  | 
 | ||||||
|  | switch sel_double  | ||||||
|  |     case 0 | ||||||
|  |         x = int64(x); | ||||||
|  |         a = int64(a); | ||||||
|  |         b = int64(b); | ||||||
|  |         y(1) = int64(YStartState); | ||||||
|  |     case 1 | ||||||
|  |         x = double(x); | ||||||
|  |         a = double(a); | ||||||
|  |         b = double(b); | ||||||
|  |         y(1) = double(YStartState); | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | switch sel_double  | ||||||
|  | 
 | ||||||
|  |     case 0 | ||||||
|  | 
 | ||||||
|  |         for i = 1:len-1        | ||||||
|  |             y_tmp(i+1) = a*x(i+1) - b*y(i)'; | ||||||
|  |             y(i+1) = bitsra(y_tmp(i+1),20) + bitget(y_tmp(i+1),64);        | ||||||
|  |         end | ||||||
|  |         out = bitsra(y,16); | ||||||
|  | 
 | ||||||
|  |     case 1 | ||||||
|  | 
 | ||||||
|  |         for i = 1:len-1 | ||||||
|  |             y_tmp(i+1) = floor(a*x(i+1) - b*y(i)'); | ||||||
|  |             y(i+1) = floor(y_tmp(i+1)/2^20) + bitget(int64(y_tmp(i+1)),64);     | ||||||
|  |         end | ||||||
|  |         out = floor(y/2^16); | ||||||
|  | end | ||||||
|  | @ -0,0 +1,30 @@ | ||||||
|  | clc;clear;close all | ||||||
|  | 
 | ||||||
|  | cd('/data/work/thfu/TailCorr/script_m'); | ||||||
|  | 
 | ||||||
|  | a = 13740916; | ||||||
|  | b = -1047703; | ||||||
|  | y(1) = 0; | ||||||
|  | 
 | ||||||
|  | sel_double = 0; | ||||||
|  | 
 | ||||||
|  | switch sel_double | ||||||
|  |     case 0 | ||||||
|  |         iir_in = int64(importdata("/home/thfu/work/TailCorr/v02/sim/in") -32768); | ||||||
|  |         iir_out = int64(importdata("/home/thfu/work/TailCorr/v02/sim/out") -32768); | ||||||
|  |     case 1 | ||||||
|  |         iir_in = double(importdata("/home/thfu/work/TailCorr/v02/sim/in") -32768); | ||||||
|  |         iir_out = double(importdata("/home/thfu/work/TailCorr/v02/sim/out") -32768); | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | Script_out = MyIIR(a,b,iir_in,y(1),sel_double)'; | ||||||
|  | y_revised = iir_in + Script_out; | ||||||
|  | tau = finddelay(y_revised,iir_out); | ||||||
|  | 
 | ||||||
|  | Script_outPhi = cat(1,zeros(1,tau)',Script_out(1:end-tau,1)); | ||||||
|  | 
 | ||||||
|  | n = 1:length(iir_in); | ||||||
|  | diff = iir_out(n)-Script_outPhi(n); | ||||||
|  | 
 | ||||||
|  | diff_plot(iir_out, Script_outPhi) | ||||||
|  | tau | ||||||
|  | @ -0,0 +1,44 @@ | ||||||
|  | clc;clear all;close all | ||||||
|  | 
 | ||||||
|  | fs = 1e8; | ||||||
|  | t = (0:1:1e2)'/fs; | ||||||
|  | % f = 20e6; | ||||||
|  | % s = sin(2*pi*f*t); | ||||||
|  | s = triang(1e2-1); | ||||||
|  | s = [zeros(1,10) s' zeros(1,10)]'; | ||||||
|  | N = length(s); | ||||||
|  | ts = (0:1:N-1)'/fs; | ||||||
|  | %%% | ||||||
|  | s_r1 = [0 s(1:end-1,1)']'; | ||||||
|  | [s2,s2_mean] = MeanIntp(s,s_r1); | ||||||
|  | 
 | ||||||
|  | s_intp2 = zeros(2*N,1); | ||||||
|  | s_intp2(1:2:2*N) = s2_mean; | ||||||
|  | s_intp2(2:2:2*N) = s2; | ||||||
|  | t_intp2 = (0:1:2*N-1)'/fs/2; | ||||||
|  | %%% | ||||||
|  | s2_r1 = [0 s2(1:end-1,1)']'; | ||||||
|  | 
 | ||||||
|  | [s4_4,s4_3] = MeanIntp(s2,s2_mean); | ||||||
|  | [s4_2,s4_1] = MeanIntp(s2_mean,s2_r1); | ||||||
|  | 
 | ||||||
|  | s_intp4 = zeros(4*N,1); | ||||||
|  | s_intp4(1:4:4*N) = s4_1; | ||||||
|  | s_intp4(2:4:4*N) = s4_2; | ||||||
|  | s_intp4(3:4:4*N) = s4_3; | ||||||
|  | s_intp4(4:4:4*N) = s4_4; | ||||||
|  | 
 | ||||||
|  | t_intp4 = (0:1:4*N-1)'/fs/4; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | figure('Units','normalized','Position',[0.000390625,0.517361111111111,0.49921875,0.422916666666667]); | ||||||
|  | plot(ts,s,t_intp2,s_intp2) | ||||||
|  | grid on | ||||||
|  | % xlim([0.58 0.62]*1e-6) | ||||||
|  | %  | ||||||
|  | %  | ||||||
|  | figure('Units','normalized','Position',[0.500390625,0.517361111111111,0.49921875,0.422916666666667]); | ||||||
|  | plot(t_intp4,s_intp4) | ||||||
|  | grid on | ||||||
|  | % xlim([0.58 0.62]*1e-6) | ||||||
|  | 
 | ||||||
|  | @ -0,0 +1,27 @@ | ||||||
|  | function out = TailCorr(alpha,beta,iir_in,YStartState,sel_double) | ||||||
|  | 
 | ||||||
|  | len = length(iir_in); | ||||||
|  | N = length(alpha); | ||||||
|  | 
 | ||||||
|  | iir_inR1 = cat(1,0,iir_in(1:end-1,1)); | ||||||
|  | 
 | ||||||
|  | diff = iir_in - iir_inR1; | ||||||
|  | 
 | ||||||
|  | Ystart = YStartState; | ||||||
|  | 
 | ||||||
|  | for i = 1:1:N | ||||||
|  |     y(1:len,i) = MyIIR(alpha(i),beta(i),diff,Ystart,sel_double); | ||||||
|  |     y = floor(y); | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | switch sel_double  | ||||||
|  | 
 | ||||||
|  |     case 0 | ||||||
|  |         y_sum = int64(sum(y,2));       | ||||||
|  | 
 | ||||||
|  |     case 1 | ||||||
|  |         y_sum = double(sum(y,2));       | ||||||
|  | 
 | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | out = iir_in+y_sum; | ||||||
|  | @ -0,0 +1,27 @@ | ||||||
|  | clc;clear;close all | ||||||
|  | 
 | ||||||
|  | cd('/data/work/thfu/TailCorr/script_m'); | ||||||
|  | 
 | ||||||
|  | sel_double = 1; | ||||||
|  | 
 | ||||||
|  | switch sel_double | ||||||
|  |     case 0 | ||||||
|  |         iir_in = int64(importdata("/home/thfu/work/TailCorr/v02/sim/in") -32768); | ||||||
|  |         iir_out = int64(importdata("/home/thfu/work/TailCorr/v02/sim/out") -32768); | ||||||
|  |     case 1 | ||||||
|  |         iir_in = double(importdata("/home/thfu/work/TailCorr/v02/sim/in") -32768); | ||||||
|  |         iir_out = double(importdata("/home/thfu/work/TailCorr/v02/sim/out") -32768); | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | alpha = [1757225200, 1045400392, 13740916]; | ||||||
|  | beta = -[1042856 1046395 1047703]; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | Ystart = 0; | ||||||
|  | 
 | ||||||
|  | y_revised = TailCorr(alpha,beta,iir_in,Ystart,sel_double); | ||||||
|  | 
 | ||||||
|  | tau = finddelay(y_revised,iir_out); | ||||||
|  | y_revisedPhi = cat(1,zeros(1,tau)',y_revised(1:end-tau,1)); | ||||||
|  | 
 | ||||||
|  | diff_plot(iir_out, y_revisedPhi,'verdi','matlab',[0 1e4]) | ||||||
|  | @ -0,0 +1,72 @@ | ||||||
|  | clc;clear;close all | ||||||
|  | 
 | ||||||
|  | cd('/data/work/thfu/TailCorr/script_m'); | ||||||
|  | 
 | ||||||
|  | sel_double = 1; | ||||||
|  | 
 | ||||||
|  | switch sel_double | ||||||
|  | 
 | ||||||
|  |     case 0 | ||||||
|  |         iir_in     = int64(importdata("/home/thfu/work/TailCorr/Test/sim/in") -32768); | ||||||
|  |         iir_out_x1 = int64(importdata("/home/thfu/work/TailCorr/Test/sim/X1_data.dat") -32768); | ||||||
|  |         iir_out_x2 = int64(importdata("/home/thfu/work/TailCorr/Test/sim/X2_data.dat") -32768); | ||||||
|  |         iir_out_x4 = int64(importdata("/home/thfu/work/TailCorr/Test/sim/X4_data.dat") -32768); | ||||||
|  |     case 1 | ||||||
|  |         iir_in     = double(importdata("/home/thfu/work/TailCorr/Test/sim/in") + 0); | ||||||
|  |         iir_out_x1 = double(importdata("/home/thfu/work/TailCorr/Test/sim/X1_data.dat") - 32768); | ||||||
|  |         iir_out_x2 = double(importdata("/home/thfu/work/TailCorr/Test/sim/X2_data.dat") - 32768); | ||||||
|  |         iir_out_x4 = double(importdata("/home/thfu/work/TailCorr/Test/sim/X4_data.dat") - 32768); | ||||||
|  |          | ||||||
|  | end | ||||||
|  | % iir_out_x1 = [iir_out_x1' zeros(1,2)]'; | ||||||
|  | % iir_out_x2 = [iir_out_x2' zeros(1,3)]'; | ||||||
|  | % iir_out_x4 = [iir_out_x4' zeros(1,6)]'; | ||||||
|  | 
 | ||||||
|  | alpha = [1757225200, 1045400392, 13740916]; | ||||||
|  | beta = -[1042856 1046395 1047703]; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | Ystart = 0; | ||||||
|  | 
 | ||||||
|  | y_revised = TailCorr(alpha,beta,iir_in,Ystart,sel_double); | ||||||
|  | 
 | ||||||
|  | s = round(y_revised); | ||||||
|  | N = length(s); | ||||||
|  | 
 | ||||||
|  | s_r1 = [0 s(1:end-1,1)']'; | ||||||
|  | [s2,s2_mean] = MeanIntp(s,s_r1); | ||||||
|  | s2 = floor(s2); | ||||||
|  | s2_mean = floor(s2_mean); | ||||||
|  | 
 | ||||||
|  | s2_r1 = [0 s2(1:end-1,1)']'; | ||||||
|  | [s4_4,s4_3] = MeanIntp(s2,s2_mean); | ||||||
|  | [s4_2,s4_1] = MeanIntp(s2_mean,s2_r1); | ||||||
|  | s4_1 = floor(s4_1); | ||||||
|  | s4_2 = floor(s4_2); | ||||||
|  | s4_3 = floor(s4_3); | ||||||
|  | s4_4 = floor(s4_4); | ||||||
|  | 
 | ||||||
|  | s_intp2 = zeros(2*N,1); | ||||||
|  | s_intp2(1:2:2*N) = s2_mean; | ||||||
|  | s_intp2(2:2:2*N) = s2; | ||||||
|  | 
 | ||||||
|  | s_intp4 = zeros(4*N,1); | ||||||
|  | s_intp4(1:4:4*N) = s4_1; | ||||||
|  | s_intp4(2:4:4*N) = s4_2; | ||||||
|  | s_intp4(3:4:4*N) = s4_3; | ||||||
|  | s_intp4(4:4:4*N) = s4_4; | ||||||
|  | %%% | ||||||
|  | tau1 = finddelay(y_revised,iir_out_x1); | ||||||
|  | y_revisedPhi1 = cat(1,zeros(1,tau1)',y_revised(1:end-tau1,1)); | ||||||
|  | figure('Units','normalized','Position',[0.000390625,0.517361111111111,0.49921875,0.422916666666667]); | ||||||
|  | diff_plot(iir_out_x1, y_revisedPhi1,'verdi','matlab',[0 2e4]) | ||||||
|  | 
 | ||||||
|  | tau2 = finddelay(s_intp2,iir_out_x2); | ||||||
|  | y_revisedPhi2 = cat(1,zeros(1,tau2)',s_intp2(1:end-tau2,1)); | ||||||
|  | figure('Units','normalized','Position',[0.500390625,0.517361111111111,0.49921875,0.422916666666667]); | ||||||
|  | diff_plot(iir_out_x2, y_revisedPhi2,'verdi','matlab',[0 4e4]) | ||||||
|  | 
 | ||||||
|  | tau4 = finddelay(s_intp4,iir_out_x4); | ||||||
|  | y_revisedPhi4 = cat(1,zeros(1,tau4)',s_intp4(1:end-tau4,1)); | ||||||
|  | figure('Units','normalized','Position',[0.000390625,0.034027777777778,0.49921875,0.422916666666667]); | ||||||
|  | diff_plot(iir_out_x4, y_revisedPhi4,'verdi','matlab',[0 8e4]) | ||||||
|  | @ -0,0 +1,26 @@ | ||||||
|  | function diff_plot(iir_out, Script_out,leg1,leg2,a) | ||||||
|  | 
 | ||||||
|  | N = length(iir_out); | ||||||
|  | n = 0:1:N-1; | ||||||
|  | diff = iir_out-Script_out; | ||||||
|  | 
 | ||||||
|  | subplot(211) | ||||||
|  | plot(n,iir_out,n,Script_out) | ||||||
|  | xlabel('n') | ||||||
|  | legend(leg1,leg2) | ||||||
|  | xlim(a) | ||||||
|  | title('time domain') | ||||||
|  | grid on | ||||||
|  | 
 | ||||||
|  | subplot(212) | ||||||
|  | plot(n,diff) | ||||||
|  | xlabel('n') | ||||||
|  | title('diff') | ||||||
|  | grid on | ||||||
|  | hold on | ||||||
|  | xlim(a) | ||||||
|  | 
 | ||||||
|  | [diff_max,R_mpos] = max(abs(diff)); | ||||||
|  | plot(n(R_mpos),diff(R_mpos),'r*') | ||||||
|  | text(n(R_mpos), diff(R_mpos), ['(',num2str(n(R_mpos)),',',num2str(diff(R_mpos)),')'],'color','k'); | ||||||
|  | % max(abs(diff)) | ||||||
		Loading…
	
		Reference in New Issue