source: trunk/src/filter_tps.m @ 427

Last change on this file since 427 was 389, checked in by sommeria, 12 years ago

several bugs corrected: object managing, tps filter...

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