| 1 | 1/1 | 返回列表 |
| 查看: 749 | 回復(fù): 0 | ||
172547204鐵蟲(chóng) (初入文壇)
|
[求助]
算法錯(cuò)誤,求原因和解決辦法
|
|
Sample Text:??? Attempted to access Amp(2); index out of bounds because numel(Amp)=1. Error in ==> x_svdprony at 263 xn(i)=xn(i)+Amp(2)*exp(damp(2)*(i-1)*dt)*cos(2*pi*Fre(2)*(i-1)*dt+bth(2)); Sample Text %打開(kāi)指定文件,并對(duì)信號(hào)進(jìn)行Pon分析計(jì)算 function [M, Amp, Fre, damp, Pha, main_f, main_damp, x, xc, y, Amp_Response, er, all, N, dt]=x_svdprony(x_in, dt, fL, showfigure) format long; x_in=[32.32 29.29 27.56 25.95 24.44 23.02 21.68 20.41 19.23 18.11 17.05 16.06 15.12 14.24 13.41 12.63 11.89 11.2 10.55 9.93 9.36 8.81 8.3 7.81 7.36 6.93 6.53 6.15 5.79 5.45 5.13 4.83 4.55 4.29 4.04 3.8 3.58 3.37 3.17 2.99 2.81 2.65 2.5 2.35 2.21 2.08 1.96 1.85 1.74 1.64 1.54 1.45 1.37 1.29 1.21 1.14 1.07 1.01 0.95 0.9 0.84 0.79 0.75 ]; x = x_in; cpu=cputime; N=size(x,1);%返回的是數(shù)組的行數(shù)也就是有多少個(gè)輸入數(shù)據(jù) %取N/2的整數(shù)部分為初始的P P=floor(N/2);%四舍五入取整數(shù) %去直流環(huán)節(jié) x_Sum = 0; for i=N-5:N x_Sum = x_Sum + x(i); end x_av = x_Sum / N; if x_av > 10E-10 for i=1:N x(i) = x(i) - x_av; end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %濾波環(huán)節(jié) %fL=1; fL=2; if fL>1 for i=fL+1:N x(i-fL)=0; for j=1:fL x(i-fL) = x(i-fL)+(1/fL)*x(i-j+1); end end end N=N-fL; tt=0:1:N-1; %P要求為偶數(shù) if mod(P, 2) ~= 0 P = P - 1; end P1=P; if mod(P, 2) == 0 % Generate R,生成樣本矩陣 for i=1 1for j=1 1+1ss=0; for k=P1:N-1 %前向預(yù)測(cè)誤差 ss=ss+x(k+2-j,1)*x(k+1-i,1); %后向預(yù)測(cè)誤差 %ss=ss+x(k+1-P1+i)*x(k+1-P1-1+j); %同時(shí)考慮雙向誤差 %ss=ss+x(k+2-j,1)*x(k+1-i,1)+x(k+1-P1+i)*x(k+1-P1-1+j); end R(i,j)=ss; end end % divide R into R1 and R2, and get A; R1*A=R2; for i=1 ![]() for j=1 ![]() R1(i,j)=R(i,j+1); end end for i=1 ![]() R2(i)=R(i,1); end %添加的代碼,使用SVD-TLS算法%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %首先進(jìn)行SVD分解 [u,s,v]=svd(R1); %根據(jù)奇異值確定實(shí)際的階數(shù)M M=0; %計(jì)算全部奇異值的均方和 Sum_SVD=0; for i=1 ![]() Sum_SVD = Sum_SVD+s(i,i)^2; end cur_sum = 0; i=1; while 1 - sqrt(cur_sum/Sum_SVD) > 0.000001 & i<=P cur_sum = cur_sum + s(i,i)^2; M = M + 1; i = i + 1; end %輸出預(yù)測(cè)的階數(shù)M M; %生成Sp矩陣 Sp=zeros(M+1, M+1); for j=1:M for i=1 P-1)-M+1Sp = Sp+s(j,j)^2*v(i:i+M,j)*v(i:i+M,j)'; end end %根據(jù)公式生成最佳最小二乘解 Sp_inv=inv(Sp); if isinf(Sp_inv(1,1)) == 1 Sp_inv = pinv(Sp); end a_SVD = Sp_inv(1+1:M+1, 1)/Sp_inv(1,1); P = M; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Get Zi cof_SVD(1)=1; for i=1:M cof_SVD(i+1)=a_SVD(i); end Z=roots(cof_SVD); % Get y,y should be very close to x for i=1 ![]() y(i)=x(i); end for i=P+1:N y(i)=0; end for i=P+1:N for j=1 ![]() y(i)=y(i)-a_SVD(j)*x(i-j); end end % Get B for i=1:N for j=1 ![]() Fy(i,j)=Z(j)^(i-1); end end Fz=Fy'; FH=Fy'*Fy; Fn=inv(FH); B = inv(Fy'*Fy)*Fy'*y'; % Calculate Amplitude, Phasor, Frequency and damp dt=0.01; for i=1 ![]() Amp(i)=abs(B(i)); Pha(i)=atan(imag(B(i))/real(B(i))); Fre(i)=atan(imag(Z(i))/real(Z(i)))/(2*pi*dt); damp(i)=log(abs(Z(i)))/dt; end %調(diào)試用的代碼 if isnan(Amp(1)) == 1 error('幅值的求解出現(xiàn)錯(cuò)誤!'); end % Get xc,verify if xc is nearly equal to x for i=1 ![]() if(real(B(i))>=0.0) bth(i)=Pha(i); else bth(i)=pi+Pha(i); end end %對(duì)Pha重新幅值 for i=1 ![]() if Pha(i) < 0 Pha(i) = Pha(i) + 2*pi; end end for i=1:N xc(i)=0.0; for j=1 ![]() xc(i)=xc(i)+Amp(j)*exp(damp(j)*(i-1)*dt)*cos(2*pi*Fre(j)*(i-1)*dt+bth(j)); end end %.........。。。自己編的代碼。。。。。。。。 xm(i)=0.0; for i=1:N xm(i)=xm(i)+Amp(1)*exp(damp(1)*(i-1)*dt)*cos(2*pi*Fre(1)*(i-1)*dt+bth(1)); end xn(i)=0.0; for i=1:N xn(i)=xn(i)+Amp(2)*exp(damp(2)*(i-1)*dt)*cos(2*pi*Fre(2)*(i-1)*dt+bth(2)); end % xk(i)=0.0; %for i=1:N % xk(i)=xk(i)+Amp(3)*exp(damp(3)*(i-1)*dt)*cos(2*pi*Fre(3)*(i-1)*dt+bth(3)); %end figure; % subplot(2,1,1); % plot(tt,x(1:N),'b', tt,y, 'r', tt,xc, '*b'); %plot(tt,xm,'r', tt,xn, 'b',tt,xc,'g',tt,x(1:N), '*b',tt,xk,'m'); plot(tt,xm,'r', tt,xn, 'b',tt,xc,'g',tt,x(1:N), '*b');%二階畫(huà)圖命令 %plot(tt,xc,'r',tt,x(1:N), '*b'); ylabel('電壓(V)'); legend('擬合的第一條指數(shù)分量','擬合的第二條指數(shù)分量','擬合曲線','實(shí)際測(cè)量值'); % legend('擬合曲線','實(shí)際測(cè)量值'); %。。。。。。。。。。自己編的代碼。。。。。。。。 % if showfigure == 1 % %顯示特征根的位置 % figure; % plot(damp, 2*pi*Fre, 'r*'); % end xj=xc'; er=0; all = 0; for i=1:N er=er+(x(i)-xc(i))^2; all = all+x(i)^2; end SNR=-20*log(sqrt(er/all)); % Calculate Prony spectrum %頻譜的取值范圍為0-5Hz f=0:0.01:4.99; for j=1:size(f,2) sptr(j)=0; sptr1(j)=0; sptr2(j)=0; angl(j)=0; tmpangle = 0; for k=1 ![]() sptr1(j)=sptr1(j)+Amp(k)*cos(bth(k))*(2*damp(k)/(damp(k)^2+(2*pi*(f(j)-Fre(k)))^2)); sptr2(j)=sptr2(j)+Amp(k)*sin(bth(k))*(2*damp(k)/(damp(k)^2+(2*pi*(f(j)-Fre(k)))^2)); end sptr(j)=sqrt(sptr1(j)^2+sptr2(j)^2); tmpangle = atan2(sptr1(j),sptr2(j)); angl(j) = tmpangle*360/pi; end %對(duì)幅頻響應(yīng)進(jìn)行歸一化,并且尋找主導(dǎo)頻率模式 %尋找sptr的最大值 max_sptr=0; main_f=0; for j=1:size(f,2) if sptr(j) > max_sptr max_sptr = sptr(j); %主導(dǎo)頻率出現(xiàn)在最大幅頻響應(yīng)位置 if f(j) ~= 0; main_f = f(j); else max_sptr = 0; end end end main_f; %找出真正計(jì)算的頻率值 f_err = 10; f_main = 0; for i=1:size(Amp, 2) if abs(main_f - Fre(i)) < f_err f_main = Fre(i); damp_main = damp(i); f_err = abs(main_f - Fre(i)); end end main_f = f_main; main_damp = damp_main; %進(jìn)行歸一化 for j=1:size(f,2) sptr(j)=sptr(j)/max_sptr; Amp_Response(j) = sptr(j); end for i=1:size(f,2) if angl(i) > 0 angl(i)=angl(i)-360; end end showfigure = 1; if showfigure == 1 % %顯示特征根的位置 % figure; % plot(damp, 2*pi*Fre, 'r*'); % end % if showfigure == 1 %在一個(gè)圖上顯示時(shí)域擬和曲線和Prony頻譜分布 % figure; % subplot(2,1,1); %plot(tt,x(1:N),'b', tt,y, 'r', tt,xc, '*b'); % plot(tt,x(1:N),'r', tt,xc, '*b'); %修改的 subplot(2,1,2); %修改的 plot(f,sptr); %subplot(2,2,3); %plot(f,sptr); %subplot(2,2,4); % plot(f,angl); end cpu=cputime-cpu; format short; end end |
找到一些相關(guān)的精華帖子,希望有用哦~
| 1 | 1/1 | 返回列表 |
| 最具人氣熱帖推薦 [查看全部] | 作者 | 回/看 | 最后發(fā)表 | |
|---|---|---|---|---|
|
[考研] 085410人工智能專(zhuān)碩317求調(diào)劑(0854都可以) +3 | xbxudjdn 2026-03-18 | 3/150 |
|
|---|---|---|---|---|
|
[考研] 0703化學(xué)求調(diào)劑 總分331 +3 | ZY-05 2026-03-13 | 3/150 |
|
|
[考研] 材料,紡織,生物(0856、0710),化學(xué)招生啦 +3 | Eember. 2026-03-17 | 9/450 |
|
|
[考研] 265求調(diào)劑 +3 | 梁梁校校 2026-03-17 | 3/150 |
|
|
[考博] 26博士申請(qǐng) +3 | 1042136743 2026-03-17 | 3/150 |
|
|
[考研] 290求調(diào)劑 +6 | 孔志浩 2026-03-12 | 11/550 |
|
|
[考研] 機(jī)械專(zhuān)碩325,尋找調(diào)劑院校 +3 | y9999 2026-03-15 | 5/250 |
|
|
[考研] 304求調(diào)劑 +5 | 素年祭語(yǔ) 2026-03-15 | 5/250 |
|
|
[考研] 085600調(diào)劑 +5 | 漾漾123sun 2026-03-12 | 6/300 |
|
|
[考研] 085600材料與化工 求調(diào)劑 +13 | enenenhui 2026-03-13 | 14/700 |
|
|
[考研] 327求調(diào)劑 +6 | 拾光任染 2026-03-15 | 11/550 |
|
|
[考研] 求老師收留調(diào)劑 +4 | jiang姜66 2026-03-14 | 5/250 |
|
|
[考研] 0856專(zhuān)碩279求調(diào)劑 +5 | 加油加油!? 2026-03-15 | 5/250 |
|
|
[考研] 中科大材料與化工319求調(diào)劑 +3 | 孟鑫材料 2026-03-14 | 3/150 |
|
|
[考研] 297求調(diào)劑 +4 | 學(xué)海漂泊 2026-03-13 | 4/200 |
|
|
[考研] 266求調(diào)劑 +4 | 學(xué)員97LZgn 2026-03-13 | 4/200 |
|
|
[考研] 0856材料與化工301求調(diào)劑 +5 | 奕束光 2026-03-13 | 5/250 |
|
|
[考研] 考研調(diào)劑 +4 | 芬達(dá)46 2026-03-12 | 4/200 |
|
|
[考研] 328化工專(zhuān)碩求調(diào)劑 +4 | 。,。,。,。i 2026-03-12 | 4/200 |
|
|
[考研] 085600材料與化工 309分請(qǐng)求調(diào)劑 +7 | dtdxzxx 2026-03-12 | 8/400 |
|