source: trunk/src/filter_tps.m @ 491

Last change on this file since 491 was 476, checked in by sommeria, 12 years ago

bugs corrected +
* filter parameter multiplied by 1000 in filter_tps*

File size: 6.2 KB
Line 
1%'filter_tps': find the thin plate spline coefficients for interpolation-smoothing
2%------------------------------------------------------------------------
3% [SubRange,NbSites,Coord_tps,U_tps,V_tps,W_tps,U_smooth,V_smooth,W_smooth,FF] =filter_tps(Coord,U,V,W,SubDomain,Rho,Threshold)
4%
5% OUTPUT:
6% SubRange(NbCoord,NbSubdomain,2): range (min, max) of the coordiantes x and y respectively, for each subdomain
7% NbSites(NbSubdomain): number of source points for each subdomain
8% FF: false flags
9% U_smooth, V_smooth: filtered velocity components at the positions of the initial data
10% Coord_tps(NbSites,NbCoord,NbSubdomain): positions of the tps centres
11% U_tps,V_tps: weight of the tps for each subdomain
12%
13% INPUT:
14% coord=[X Y]: matrix whose first column is the x coordinates of the initial data, the second column the y coordiantes
15% U,V: set of velocity components of the initial data
16% Rho: smoothing parameter
17% Threshold: max diff accepted between smoothed and initial data
18% Subdomain: estimated number of data points in each subdomain
19
20function [SubRange,NbSites,Coord_tps,U_tps,V_tps,W_tps,U_smooth,V_smooth,W_smooth,FF] =filter_tps(Coord,U,V,W,SubDomain,Rho,Threshold)
21%subdomain decomposition
22warning off
23nbvec=size(Coord,1);
24W_tps=[];%default
25W_smooth=[];
26NbCoord=size(Coord,2);
27NbSubDomain=nbvec/SubDomain;
28MinCoord=min(Coord,[],1);
29MaxCoord=max(Coord,[],1);
30Range=MaxCoord-MinCoord;
31AspectRatio=Range(2)/Range(1);
32NbSubDomainX=max(floor(sqrt(NbSubDomain/AspectRatio)),1);
33NbSubDomainY=max(floor(sqrt(NbSubDomain*AspectRatio)),1);
34NbSubDomain=NbSubDomainX*NbSubDomainY;
35Siz(1)=Range(1)/NbSubDomainX;%width of subdomains
36Siz(2)=Range(2)/NbSubDomainY;%height of subdomains
37CentreX=linspace(MinCoord(1)+Siz(1)/2,MaxCoord(1)-Siz(1)/2,NbSubDomainX);
38CentreY=linspace(MinCoord(2)+Siz(2)/2,MaxCoord(2)-Siz(2)/2,NbSubDomainY);
39[CentreX,CentreY]=meshgrid(CentreX,CentreY);
40CentreY=reshape(CentreY,1,[]);% Y positions of subdomain centres
41CentreX=reshape(CentreX,1,[]);% X positions of subdomain centres
42rho=Siz(1)*Siz(2)*Rho/1000;%optimum rho increase as the area of the subdomain (division by 10^6 to reach good values with the default GUI input)
43U_tps_sub=zeros(nbvec,NbSubDomain);%default spline
44V_tps_sub=zeros(nbvec,NbSubDomain);%default spline
45Indices_tps=zeros(nbvec,NbSubDomain);%default indices
46U_smooth=zeros(nbvec,1);
47V_smooth=zeros(nbvec,1);
48nb_select=zeros(nbvec,1);
49FF=zeros(nbvec,1);
50check_empty=zeros(1,NbSubDomain);
51SubRange=zeros(NbCoord,2,NbSubDomain);%initialise the positions of subdomains
52% SubRangy=zeros(NbSubDomain,2);
53for isub=1:NbSubDomain
54    SubRange(1,:,isub)=[CentreX(isub)-0.55*Siz(1) CentreX(isub)+0.55*Siz(1)];
55    SubRange(2,:,isub)=[CentreY(isub)-0.55*Siz(2) CentreY(isub)+0.55*Siz(2)];
56    ind_sel_previous=[];
57    ind_sel=0;
58    while numel(ind_sel)>numel(ind_sel_previous) %increase the subdomain during four iterations at most
59        ind_sel_previous=ind_sel;
60        ind_sel=find(Coord(:,1)>=SubRange(1,1,isub) & Coord(:,1)<=SubRange(1,2,isub) & Coord(:,2)>=SubRange(2,1,isub) & Coord(:,2)<=SubRange(2,2,isub));
61        % if no vector in the subdomain, skip the subdomain
62        if isempty(ind_sel)
63            check_empty(isub)=1;   
64            U_tps(1,isub)=0;%define U_tps and V_tps by default
65            V_tps(1,isub)=0;
66            break
67            % if too few selected vectors, increase the subrange for next iteration
68        elseif numel(ind_sel)<SubDomain/4 && ~isequal( ind_sel,ind_sel_previous);
69            SubRange(:,1,isub)=SubRange(:,1,isub)-Siz'/4;
70            SubRange(:,2,isub)=SubRange(:,2,isub)+Siz'/4;
71        else         
72            [U_smooth_sub,U_tps_sub]=tps_coeff(Coord(ind_sel,:),U(ind_sel),rho);
73            [V_smooth_sub,V_tps_sub]=tps_coeff(Coord(ind_sel,:),V(ind_sel),rho);
74            UDiff=U_smooth_sub-U(ind_sel);
75            VDiff=V_smooth_sub-V(ind_sel);
76            NormDiff=UDiff.*UDiff+VDiff.*VDiff;
77            ind_ind_sel=1:numel(ind_sel);%default
78            if exist('Threshold','var')
79            FF(ind_sel)=20*(NormDiff>Threshold);%put FF value to 20 to identify the criterium of elimmination
80            ind_ind_sel=find(FF(ind_sel)==0); % select the indices of ind_sel corresponding to the remaining vectors
81            end
82            % if no value exceeds threshold, the result is recorded
83            if isequal(numel(ind_ind_sel),numel(ind_sel))
84                U_smooth(ind_sel)=U_smooth(ind_sel)+U_smooth_sub;
85                V_smooth(ind_sel)=V_smooth(ind_sel)+V_smooth_sub;
86                NbSites(isub)=numel(ind_sel);
87                Coord_tps(1:NbSites(isub),:,isub)=Coord(ind_sel,:);
88                U_tps(1:NbSites(isub)+3,isub)=U_tps_sub;
89                V_tps(1:NbSites(isub)+3,isub)=V_tps_sub;         
90                nb_select(ind_sel)=nb_select(ind_sel)+1;
91                display('good')
92                break
93            % if too few selected vectors, increase the subrange for next iteration
94            elseif numel(ind_ind_sel)<SubDomain/4 && ~isequal( ind_sel,ind_sel_previous);
95                SubRange(:,1,isub)=SubRange(:,1,isub)-Siz'/4;
96                SubRange(:,2,isub)=SubRange(:,2,isub)+Siz'/4;
97            % else interpolation-smoothing is done again with the selected vectors
98            else
99                [U_smooth_sub,U_tps_sub]=tps_coeff(Coord(ind_sel(ind_ind_sel),:),U(ind_sel(ind_ind_sel)),rho);
100                [V_smooth_sub,V_tps_sub]=tps_coeff(Coord(ind_sel(ind_ind_sel),:),V(ind_sel(ind_ind_sel)),rho);
101                U_smooth(ind_sel(ind_ind_sel))=U_smooth(ind_sel(ind_ind_sel))+U_smooth_sub;
102                V_smooth(ind_sel(ind_ind_sel))=V_smooth(ind_sel(ind_ind_sel))+V_smooth_sub;   
103                NbSites(isub)=numel(ind_ind_sel);
104                Coord_tps(1:NbSites(isub),:,isub)=Coord(ind_sel(ind_ind_sel),:);
105                U_tps(1:NbSites(isub)+3,isub)=U_tps_sub;
106                V_tps(1:NbSites(isub)+3,isub)=V_tps_sub;
107                nb_select(ind_sel(ind_ind_sel))=nb_select(ind_sel(ind_ind_sel))+1;
108                display('good2')
109                break
110            end
111        end
112    end
113end
114ind_empty=find(check_empty);
115%remove empty subdomains
116if ~isempty(ind_empty)
117    SubRange(:,:,ind_empty)=[];
118    Coord_tps(:,:,ind_empty)=[];
119    U_tps(:,ind_empty)=[];
120    V_tps(:,ind_empty)=[];
121end
122nb_select(nb_select==0)=1;%ones(size(find(nb_select==0)));
123U_smooth=U_smooth./nb_select;
124V_smooth=V_smooth./nb_select;
Note: See TracBrowser for help on using the repository browser.