Wiener Filter
Forums › ProRealTime English forum › ProBuilder support › Wiener Filter
- This topic has 7 replies, 4 voices, and was last updated 8 years ago by Bandido.
-
-
06/16/2016 at 6:42 AM #9437
Hi.
Is somebody able to write the code for the optimum Wiener filter?
Best.
Renato
06/16/2016 at 8:47 AM #9440Hello Renato,
Why not, I just googled it and didn’t find any other trading platform formula that could be easily converted.
Are you able to tell us more details about it and how you’d like it to operate on market price? Thanks.
06/16/2016 at 10:47 AM #9448Hi Nicolas.
The Wiener filter minimizes the mean square error between the estimated random process and the desired process; it is optimum in this sense: there is no other filter that can extract the signal (the trend, or sideways… in our case) out of the noise in a better way, given certain assumptions on the statistical structure of the noise etc.
The formulas are a little hawkward, and can be found here: https://en.wikipedia.org/wiki/Wiener_filter
Best
Renato
06/16/2016 at 3:27 PM #9456I can help you, it’s very interesting but I do not understand why this kind of filter has never been re-coded for market data in the past..
At least, a C++ function or any computerized language of any kind of this math formula would be of great help to recode it for PRT. Thanks.
06/16/2016 at 4:18 PM #9461I think this is because the formulas are of a higher level of difficulty than is normal for standard technical analysis
best
06/17/2016 at 12:46 PM #949706/17/2016 at 1:05 PM #9499Hi
this is what happens normally with technical analysis; in this case the math is quite complicated: https://en.wikipedia.org/wiki/Wiener_filter
and I’m not sure PRT has the capability to encode it
best
renato
07/08/2016 at 3:51 PM #10319I have found the matlab code here:
http://freesourcecode.net/sites/default/files/57424.zip
this is the code:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276function Project%Author: Alex Dytso%Description: This on of the project that shows how to implement Wiener%filter as noise cancellations.% Our desired response is d(n) ouptut is x(n). We have noise v(n), v1(n)% and v2(n)% that have the following relationship v1(n)=0.8*v1(n-1)+v(n) and v2(n)=-0.6v2(n-1)+v(n)% where v(n) is AWGN.% The ouptut is x(n)=d(n)+v1(n)clc, clear all, close all%%%%%Part2theta=2*pi*rand-pi; % generating random varible thetha between -pi and piw0=0.05*pi;N=10000;n=1:N;dn=sin(n*w0+theta); %generating our siganlvn=randn(1,N); %generating 10000 points of White Gaussian Noise% generating v1n=0.8*v1(n-1)+v(n) and v2(n)=-0.6v2(n-1)+v(n)v1(1)=vn(1);v2(1)=vn(1);for i=2:Nv1(i)=0.1*v1(i-1)+vn(i);v2(i)=-0.05*v2(i-1)+vn(i);end%generating x(n)=d(n)+v1(n)xn=dn+v1;%%%%%Part 3%ploting xn and dnfigure(1)plot(1:200,xn(1:200))hold onplot(1:200,dn(1:200),'r')xlabel('n')legend('xn','dn')title('Plot of x(n) and d(n)')%ploting v2nfigure(2)plot(1:200,v2(1:200))xlabel('n')title('Plot of v_2(n)')%%%%% part 4 Computing auto-correlation of v2n%comparing computatation with my function to computation with matlabrv2=AUTOCORR(v2(1:40)) %computation with my functionrv2m=xcorr(v2(1:40),'biased'); %computation with MatLabfigure(3)stem(rv2)hold onstem(rv2m(20:39),'xr')xlabel('k')title('Autocorrlation of v_2(n)')legend('My function', 'MatLab function')%%%% part5 Computing cross-correlation between xn and v2n%comparing computation via my function with computation via MatLabrxv2=CROSSCORR(xn(1:50),v2(1:50)) %my functionrxv2m=xcorr(xn(1:50),v2(1:50),'biased'); %MatLab functionfigure(4)stem(rxv2)hold onstem(rxv2m(20:39),'xr')xlabel('k')title('Cross-correlation between x(n) and v_2(n)')legend('My function', 'MatLab function')%%%% part 6 Finding Coefficient of Wiener filter of order 1%Coefficient are compute by w=Rv2^(-1)*rxv2Rv2=[rv2(1),rv2(2);rv2(2),rv2(1)] %Autocorrelation matrix of v2w=(Rv2)^(-1)*rxv2(1:2) %Coefficients of Wiener fileter of oreder 1v1nEst=conv(w,v2); %filteringdest=xn-v1nEst(1:N); %subtracting estimate of v1 from xnfigure(5)plot(dest(1:200))hold onplot(dn(1:200),'r')xlabel('n')title('Estimated d(n) vs actual d(n). Filter order 1')legend('Estimate d(n)', 'Actual d(n)')%%%%part 7 order 6% autocorrelation matrixRv2ord6=[rv2(1),rv2(2),rv2(3),rv2(4),rv2(5),rv2(6),rv2(7);rv2(2),rv2(1),rv2(2),rv2(3),rv2(4),rv2(5),rv2(6);rv2(3),rv2(2),rv2(1),rv2(2),rv2(3),rv2(4),rv2(5);rv2(4),rv2(3),rv2(2),rv2(1),rv2(2),rv2(3),rv2(4);rv2(5),rv2(4),rv2(3),rv2(2),rv2(1),rv2(2),rv2(3);rv2(6),rv2(5),rv2(4),rv2(3),rv2(2),rv2(1),rv2(2);rv2(7),rv2(6),rv2(5),rv2(4),rv2(3),rv2(2),rv2(1)]word6=inv(Rv2ord6)*rxv2(1:7)%computing optimal coefficientsv1nEst6=conv(word6,v2);%filtering with Wiener filterdest6=xn-v1nEst6(1:10000);%computing the errorfigure(6)plot(dest6(1:200))hold onplot(dn(1:200),'r')xlabel('n')title('Estimated d(n) vs actual d(n). Filter order 6')legend('Estimate d(n)', 'Actual d(n)')%%%part 8% autocorrelation matrixRv2ord12=[rv2(1),rv2(2),rv2(3),rv2(4),rv2(5),rv2(6),rv2(7),rv2(8),rv2(9),rv2(10),rv2(11),rv2(12),rv2(13);rv2(2),rv2(1),rv2(2),rv2(3),rv2(4),rv2(5),rv2(6),rv2(7),rv2(8),rv2(9),rv2(10),rv2(11),rv2(12);rv2(3),rv2(2),rv2(1),rv2(2),rv2(3),rv2(4),rv2(5),rv2(6),rv2(7),rv2(8),rv2(9),rv2(10),rv2(11);rv2(4),rv2(3),rv2(2),rv2(1),rv2(2),rv2(3),rv2(4),rv2(5),rv2(6),rv2(7),rv2(8),rv2(9),rv2(10);rv2(5),rv2(4),rv2(3),rv2(2),rv2(1),rv2(2),rv2(3),rv2(4),rv2(5),rv2(6),rv2(7),rv2(8),rv2(9);rv2(6),rv2(5),rv2(4),rv2(3),rv2(2),rv2(1),rv2(2),rv2(3),rv2(4),rv2(5),rv2(6),rv2(7),rv2(8);rv2(7),rv2(6),rv2(5),rv2(4),rv2(3),rv2(2),rv2(1),rv2(2),rv2(3),rv2(4),rv2(5),rv2(6),rv2(7);rv2(8),rv2(7),rv2(6),rv2(5),rv2(4),rv2(3),rv2(2),rv2(1),rv2(2),rv2(3),rv2(4),rv2(5),rv2(6);rv2(9),rv2(8),rv2(7),rv2(6),rv2(5),rv2(4),rv2(3),rv2(2),rv2(1),rv2(2),rv2(3),rv2(4),rv2(5);rv2(10),rv2(9),rv2(8),rv2(7),rv2(6),rv2(5),rv2(4),rv2(3),rv2(2),rv2(1),rv2(2),rv2(3),rv2(4);rv2(11),rv2(10),rv2(9),rv2(8),rv2(7),rv2(6),rv2(5),rv2(4),rv2(3),rv2(2),rv2(1),rv2(2),rv2(3);rv2(12),rv2(11),rv2(10),rv2(9),rv2(8),rv2(7),rv2(6),rv2(5),rv2(4),rv2(3),rv2(2),rv2(1),rv2(2);rv2(13),rv2(12),rv2(11),rv2(10),rv2(9),rv2(8),rv2(7),rv2(6),rv2(5),rv2(4),rv2(3),rv2(2),rv2(1)]word12=inv(Rv2ord12)*rxv2(1:13) %computing optimal coefficientsv1nEst12f=filter(word12,1,v2); %filtering with Wiener filterdest12=xn-v1nEst12f(1:10000); %computing the errorfigure(7)plot(dest12(1:200))hold onplot(dn(1:200),'r')xlabel('n')title('Estimated d(n) vs actual d(n). Filter order 12')legend('Estimate d(n) ', 'Actual d(n)')%%%%part 9 (EXTRA FILTER ORDER 20)Rv2ord20= [rv2(1),rv2(2),rv2(3),rv2(4),rv2(5),rv2(6),rv2(7),rv2(8),rv2(9),rv2(10),rv2(11),rv2(12),rv2(13),rv2(14),rv2(15),rv2(16),rv2(17),rv2(18),rv2(19),rv2(20);rv2(2),rv2(1),rv2(2),rv2(3),rv2(4),rv2(5),rv2(6),rv2(7),rv2(8),rv2(9),rv2(10),rv2(11),rv2(12),rv2(13),rv2(14),rv2(15),rv2(16),rv2(17),rv2(18),rv2(19);rv2(3),rv2(2),rv2(1),rv2(2),rv2(3),rv2(4),rv2(5),rv2(6),rv2(7),rv2(8),rv2(9),rv2(10),rv2(11),rv2(12),rv2(13),rv2(14),rv2(15),rv2(16),rv2(17),rv2(18);rv2(4),rv2(3),rv2(2),rv2(1),rv2(2),rv2(3),rv2(4),rv2(5),rv2(6),rv2(7),rv2(8),rv2(9),rv2(10),rv2(11),rv2(12),rv2(13),rv2(14),rv2(15),rv2(16),rv2(17);rv2(5),rv2(4),rv2(3),rv2(2),rv2(1),rv2(2),rv2(3),rv2(4),rv2(5),rv2(6),rv2(7),rv2(8),rv2(9),rv2(10),rv2(11),rv2(12),rv2(13),rv2(14),rv2(15),rv2(16);rv2(6),rv2(5),rv2(4),rv2(3),rv2(2),rv2(1),rv2(2),rv2(3),rv2(4),rv2(5),rv2(6),rv2(7),rv2(8),rv2(9),rv2(10),rv2(11),rv2(12),rv2(13),rv2(14),rv2(15);rv2(7),rv2(6),rv2(5),rv2(4),rv2(3),rv2(2),rv2(1),rv2(2),rv2(3),rv2(4),rv2(5),rv2(6),rv2(7),rv2(8),rv2(9),rv2(10),rv2(11),rv2(12),rv2(13),rv2(14);rv2(8),rv2(7),rv2(6),rv2(5),rv2(4),rv2(3),rv2(2),rv2(1),rv2(2),rv2(3),rv2(4),rv2(5),rv2(6),rv2(7),rv2(8),rv2(9),rv2(10),rv2(11),rv2(12),rv2(13);rv2(9),rv2(8),rv2(7),rv2(6),rv2(5),rv2(4),rv2(3),rv2(2),rv2(1),rv2(2),rv2(3),rv2(4),rv2(5),rv2(6),rv2(7),rv2(8),rv2(9),rv2(10),rv2(11),rv2(12);rv2(10),rv2(9),rv2(8),rv2(7),rv2(6),rv2(5),rv2(4),rv2(3),rv2(2),rv2(1),rv2(2),rv2(3),rv2(4),rv2(5),rv2(6),rv2(7),rv2(8),rv2(9),rv2(10),rv2(11);rv2(11),rv2(10),rv2(9),rv2(8),rv2(7),rv2(6),rv2(5),rv2(4),rv2(3),rv2(2),rv2(1),rv2(2),rv2(3),rv2(4),rv2(5),rv2(6),rv2(7),rv2(8),rv2(9),rv2(10);rv2(12),rv2(11),rv2(10),rv2(9),rv2(8),rv2(7),rv2(6),rv2(5),rv2(4),rv2(3),rv2(2),rv2(1),rv2(2),rv2(3),rv2(4),rv2(5),rv2(6),rv2(7),rv2(8),rv2(9);rv2(13),rv2(12),rv2(11),rv2(10),rv2(9),rv2(8),rv2(7),rv2(6),rv2(5),rv2(4),rv2(3),rv2(2),rv2(1),rv2(2),rv2(3),rv2(4),rv2(5),rv2(6),rv2(7),rv2(8);rv2(14),rv2(13),rv2(12),rv2(11),rv2(10),rv2(9),rv2(8),rv2(7),rv2(6),rv2(5),rv2(4),rv2(3),rv2(2),rv2(1),rv2(2),rv2(3),rv2(4),rv2(5),rv2(6),rv2(7);rv2(15),rv2(14),rv2(13),rv2(12),rv2(11),rv2(10),rv2(9),rv2(8),rv2(7),rv2(6),rv2(5),rv2(4),rv2(3),rv2(2),rv2(1),rv2(2),rv2(3),rv2(4),rv2(5),rv2(6);rv2(16),rv2(15),rv2(14),rv2(13),rv2(12),rv2(11),rv2(10),rv2(9),rv2(8),rv2(7),rv2(6),rv2(5),rv2(4),rv2(3),rv2(2),rv2(1),rv2(2),rv2(3),rv2(4),rv2(5);rv2(17),rv2(16),rv2(15),rv2(14),rv2(13),rv2(12),rv2(11),rv2(10),rv2(9),rv2(8),rv2(7),rv2(6),rv2(5),rv2(4),rv2(3),rv2(2),rv2(1),rv2(2),rv2(3),rv2(4);rv2(18),rv2(17),rv2(16),rv2(15),rv2(14),rv2(13),rv2(12),rv2(11),rv2(10),rv2(9),rv2(8),rv2(7),rv2(6),rv2(5),rv2(4),rv2(3),rv2(2),rv2(1),rv2(2),rv2(3);rv2(19),rv2(18),rv2(17),rv2(16),rv2(15),rv2(14),rv2(13),rv2(12),rv2(11),rv2(10),rv2(9),rv2(8),rv2(7),rv2(6),rv2(5),rv2(4),rv2(3),rv2(2),rv2(1),rv2(2);rv2(20),rv2(19),rv2(18),rv2(17),rv2(16),rv2(15),rv2(14),rv2(13),rv2(12),rv2(11),rv2(10),rv2(9),rv2(8),rv2(7),rv2(6),rv2(5),rv2(4),rv2(3),rv2(2),rv2(1)];word19=inv(Rv2ord20)*rxv2(1:20)v1nEst19=conv(word19,v2);dest19=xn-v1nEst19(1:10000);figure(8)plot(dest19(1:200))hold onplot(dn(1:200),'r')xlabel('n')title('Estimated d(n) vs actual d(n). Filter order 19')legend('Estimate d(n)', 'Actual d(n)')%ETRA minimum erroren1=(dn-dest).^2;en6=(dn-dest6).^2;en12=(dn-dest12).^2;en19=(dn-dest19).^2;J1=mean(en1)J6=mean(en6)J12=mean(en12)J19=mean(en19)% figure(9)% plot(en1(1:100))% hold on% plot(en6(1:100),'r')% plot(en12(1:100),'g')% plot(en19(1:100),'y')%%%% EXTRA fftfigure(10)subplot(4,1,1)plot(1:200,fft(dest(1:200)))hold onsubplot(4,1,2)plot(1:200,fft(dest6(1:200)))subplot(4,1,3)plot(1:200,fft(dest12(1:200)))subplot(4,1,4)plot(1:200,fft(dest19(1:200)))end%%%% Autocorrelation and Cross-correlation functionsfunction y=AUTOCORR(x) %this function computes autocorrelationK=length(x);for index=1:Ky(index,:)=sum([zeros(1,index-1),x].*[x,zeros(1,index-1)])/(K);endendfunction y=CROSSCORR(X,Z) %this function computes autocorrelationx=[X,zeros(1,length(Z)-length(X))]; %Making length of x and z to be the samez=[Z,zeros(1,length(X)-length(Z))];N=length(z);for index=1:Ny(index,:)=sum([x,zeros(1,index-1)].*[zeros(1,index-1),z])/(N);endendI think that it is impossible to convert in matlab…
-
AuthorPosts