Skip to content

Commit

Permalink
Lagrange, idht, flt
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrubeck committed Jan 11, 2016
1 parent 34885bf commit 5def8ff
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 35 deletions.
3 changes: 2 additions & 1 deletion Analysis/HermFour.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function [] = HermFour(f, n, m)
[x, w]=GaussHermite(n); ww=log(sqrt(2)*w);
[x, w]=GaussHermite(n,0,1); ww=log(sqrt(2)*w);
H=zeros(m, n);
H(1,:)=pi^(-1/4)*exp(-x.^2);
H(2,:)=H(1,:).*(2*x);
Expand All @@ -21,6 +21,7 @@
sum_log_scale=sum_log_scale+log_scale;
end


F=H'*diag(1i.^(0:m-1))*W;
G=conj(F);
[kx, ky]=meshgrid(sqrt(2)*x);
Expand Down
4 changes: 2 additions & 2 deletions Analysis/sincFT2.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

z=f(xx, yy);
A=h^2/(2*pi);
u=fftshift(fft2(fftshift(z)).*A);
u=fftshift(fft2(fftshift(z))*A);
%uu=-u./(kx.^2+ky.^2); uu(N/2+1,N/2+1)=0;
v=fftshift(ifft2(fftshift(z))./A);
v=fftshift(ifft2(fftshift(z))/A);


figure(1); colormap(jet(256));
Expand Down
24 changes: 24 additions & 0 deletions DPT/flt.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function [] = flt(n)
% Fast Legendre Transform, based on Alpert-Rokhlin

% Initialization
k=32;
s=4*k;

% Step 1
r=0:k-1;
t=(1-cos((r+0.5)*pi/k))/2;
tt=[t/2, (1+t)/2];

% Step 2
T=repmat(t,[k,1]);
den=prod(T-T'+eye(k));
l=0:s-1;
U=repmat(l/s,[k,1])-repmat(t(:),[1,s]);
U=bsxfun(@rdivide, bsxfun(@rdivide, prod(U), U), den(:));

%Step 3
h=log2(n/s)-1;

end

21 changes: 11 additions & 10 deletions DPT/fpt.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function [f] = fpt(a,b,c,g)
% Fast Polynomial Transform, based on Potts (1998)
% Fast Polynomial Transform, based on Potts (1998).
% Basis exchange for orthogonal polynomials that satisfy the three term
% recurrence relation: P[n](x)=(a[n]x+b[n])P[n-1](x)+c[n]P[n-2](x).
% The last input argument, g, should be the coeffients of the linear
Expand All @@ -14,8 +14,8 @@

global fileID;
global create;

N=length(g)-1; t=log2(N);
N=length(g)-1;
t=log2(N);

filename=sprintf('fptU%d.bin',N);
create=1-exist(filename,'file')/2;
Expand All @@ -25,7 +25,8 @@
fileID=fopen(filename,'r');
end

gg=[g(:), zeros(N+1,1)]; gg(N+1,:)=[];
gg=[g(:), zeros(N+1,1)];
gg(N+1,:)=[];
gg(N-1,1)=g(N-1)+c(N)*g(N+1);
gg(N,:)=[g(N)+b(N)*g(N+1), a(N)*g(N+1)];
ggg=zeros(N,2);
Expand All @@ -38,15 +39,15 @@
j=2*i-1;
h=m*(i-1)+1;
[u11,u12,u21,u22]=calculateU(a,b,c,m-1,h,x);
ggg( i , : ) = c(h+1)*innerP(u11, u12, gg(j+2,:), gg(j+3,:));
ggg(i , : ) = c(h+1)*innerP(u11, u12, gg(j+2,:), gg(j+3,:));
ggg(i+1, : ) = innerP(u21, u22, gg(j+2,:), gg(j+3,:));
ggg( i ,1:m) = ggg( i ,1:m)+gg(j,:);
ggg(i ,1:m) = ggg(i ,1:m)+gg(j ,:);
ggg(i+1,1:m) = ggg(i+1,1:m)+gg(j+1,:);
end
gg=ggg;
end

% f=gg(1,:)+(b(1)I+a(1)T')*gg(2,:)
% f=gg(1,:)+(b(1)*I+a(1)*T')*gg(2,:)
f=zeros(size(g));
f(1)=gg(1,1)+b(1)*gg(2,1)+a(1)*gg(2,2)/2;
f(2)=gg(1,2)+b(1)*gg(2,2)+a(1)*(gg(2,1)+gg(2,3)/2);
Expand All @@ -57,6 +58,7 @@
end

function [u11, u12, u21, u22] = calculateU(a,b,c,n,j,x)
% Calculates the 4x4 matrix with polynomial entries
global fileID;
global create;
if(create)
Expand All @@ -65,8 +67,7 @@
fwrite(fileID, [u11; u12; u21; u22], 'double');
else
U=fread(fileID, [4, length(x)], 'double');
u11=U(1,:); u12=U(2,:);
u21=U(3,:); u22=U(4,:);
u11=U(1,:); u12=U(2,:); u21=U(3,:); u22=U(4,:);
end
end

Expand All @@ -84,6 +85,6 @@
% Computes polynomial vector dot product [u1; u2]'*[a1; a2]
a1(1)=a1(1)*sqrt(2);
a2(1)=a2(1)*sqrt(2);
P=dct(u1.*idct(a1, length(u1))+u2.*idct(a2, length(u2)));
P=dct(u1.*idct(a1,length(u1))+u2.*idct(a2,length(u2)));
P(1)=P(1)/sqrt(2);
end
11 changes: 11 additions & 0 deletions DPT/idht.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function [z] = idht(f, N)
% Inverse Discrete Hermite Transform
x=sqrt(pi/(2*N))*(-N:2:N-2);
H=zeros(N,N);
H(1,:)=pi^-(1/4)*exp(-x.^2/2);
H(2,:)=sqrt(2)*x.*H(1,:);
for i=1:N-2
H(i+2,:)=sqrt(2/(i+1))*x.*H(i+1,:)-sqrt(i/(i+1))*H(i,:);
end
z=sqrt(2*pi/N)*H*f(x');
end
6 changes: 3 additions & 3 deletions Integration/GaussChebyshev.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function [x, w] = GaussChebyshev(a, b, n)
% Returns abscissas and weights for the Gauss-Chebyshev n-point quadrature
% over the interval [a, b]. Weigths include reciprocal weight function.
th=((1:n)-1/2)*pi/n;
% over the interval [a, b].
th=pi*(1:2:2*n-1)/(2*n);
x=(b-a)/2*cos(th)+(a+b)/2;
w=(b-a)*pi/(2*n)*sin(th);
w(1:n)=(b-a)*pi/(2*n);
end
5 changes: 3 additions & 2 deletions Integration/GaussHermite.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
function [x, w] = GaussHermite(n)
function [x, w] = GaussHermite(n, mu, sigma)
% Returns abscissas and weights for the Gauss-Hermite n-point quadrature
% over the interval [-inf, inf] using the Golub-Welsch Algorithm.
beta=sqrt((1:n-1)/2);
[x,V]=trideigs(zeros(1,n), beta);
x=x'; w=sqrt(pi)*V(1,:).^2;
x=(sqrt(2)*sigma)*x'+mu;
w=sqrt(2*pi)*sigma*V(1,:).^2;
end
24 changes: 7 additions & 17 deletions Interpolation/Lagrange.m
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
function [p] = Lagrange(x, y, t)
% Evaluates the Lagrange interpolation polynomial.
n=length(x);
w=ones(size(x));
for i=1:n
for j=1:n
if j~=i
w(i)=w(i)*(x(i)-x(j));
end
end
end
g=1;
p=0;
for k=1:n
d=t-x(k);
p=p+y(k)./(w(k)*d);
g=g.*d;
end
p=g.*p;
n=length(x);
m=length(t);
X=repmat(x(:).',[n,1]);
w=prod(X-X.'+eye(n));
r=y./w;
D=repmat(t(:).',[n,1])-repmat(x(:),[1,m]);
p=prod(D).*(r*(1./D));
end
12 changes: 12 additions & 0 deletions Special Functions/HermitePsi.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function [y]=HermitePsi(a, x)
% Evaluates the Hermite function series given by the coefficients a(n)
n=length(a);
y=(n>1)*a(n); yy=zeros(size(x));
for k=n-2:-1:1
temp=y;
y=a(k+1)+sqrt(2/(k+1))*x.*y-sqrt((k+1)/(k+2))*yy;
yy=temp;
end
h0=pi^(-1/4)*exp(-x.^2/2);
y=h0.*(a(1)+sqrt(2)*x.*y-sqrt(1/2)*yy);
end
5 changes: 5 additions & 0 deletions Special Functions/jinc.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function [j] = jinc(a,x)
t=(x==0);
j=(besselj(a,x)+t)./(x+t);
end

5 changes: 5 additions & 0 deletions Special Functions/sinc.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function [y] = sinc(x)
t=(x==0);
y=(sin(x)+t)./(x+t);
end

Binary file modified fptU1024.bin
Binary file not shown.

0 comments on commit 5def8ff

Please sign in to comment.