source: trunk/src/filter_tps.m @ 387

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

several bugs corrected: mask, color image...

File size: 6.6 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=ceil(nbvec/SubDomain);
31MinCoord=min(Coord,[],1);
32% MinY=min(Y,);
33MaxCoord=max(Coord,[],1);
34% MaxY=max(Y);
35Range=MaxCoord-MinCoord;
36% RangY=MaxY-MinY;
37AspectRatio=Range(2)/Range(1);
38NbSubDomainX=max(floor(sqrt(NbSubDomain/AspectRatio)),1);
39NbSubDomainY=max(floor(sqrt(NbSubDomain*AspectRatio)),1);
40NbSubDomain=NbSubDomainX*NbSubDomainY;
41Siz(1)=Range(1)/NbSubDomainX;%width of subdomains
42Siz(2)=Range(2)/NbSubDomainY;%height of subdomains
43CentreX=linspace(MinCoord(1)+Siz(1)/2,MaxCoord(1)-Siz(1)/2,NbSubDomainX);
44CentreY=linspace(MinCoord(2)+Siz(2)/2,MaxCoord(2)-Siz(2)/2,NbSubDomainY);
45[CentreX,CentreY]=meshgrid(CentreX,CentreY);
46CentreY=reshape(CentreY,1,[]);% Y positions of subdomain centres
47CentreX=reshape(CentreX,1,[]);% X positions of subdomain centres
48rho=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)
49U_tps_sub=zeros(nbvec,NbSubDomain);%default spline
50V_tps_sub=zeros(nbvec,NbSubDomain);%default spline
51Indices_tps=zeros(nbvec,NbSubDomain);%default indices
52U_smooth=zeros(nbvec,1);
53V_smooth=zeros(nbvec,1);
54nb_select=zeros(nbvec,1);
55FF=zeros(nbvec,1);
56check_empty=zeros(1,NbSubDomain);
57SubRange=zeros(NbCoord,2,NbSubDomain);%initialise the positions of subdomains
58% SubRangy=zeros(NbSubDomain,2);
59for isub=1:NbSubDomain
60    SubRange(1,:,isub)=[CentreX(isub)-0.55*Siz(1) CentreX(isub)+0.55*Siz(1)];
61    SubRange(2,:,isub)=[CentreY(isub)-0.55*Siz(2) CentreY(isub)+0.55*Siz(2)];
62    ind_sel_previous=[];
63    ind_sel=0;
64    while numel(ind_sel)>numel(ind_sel_previous) %increase the subdomain during four iterations at most
65        ind_sel_previous=ind_sel;
66        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));
67        % if no vector in the subdomain, skip the subdomain
68        if isempty(ind_sel)
69            check_empty(isub)=1;   
70            U_tps(1,isub)=0;%define U_tps and V_tps by default
71            V_tps(1,isub)=0;
72            break
73            % if too few selected vectors, increase the subrange for next iteration
74        elseif numel(ind_sel)<SubDomain/4 && ~isequal( ind_sel,ind_sel_previous);
75            SubRange(:,1,isub)=SubRange(:,1,isub)-Siz'/4;
76            SubRange(:,2,isub)=SubRange(:,2,isub)+Siz'/4;
77%             SubRangy(isub,1)=SubRangy(isub,1)-Siz(2)/4;
78%             SubRangy(isub,2)=SubRangy(isub,2)+Siz(2)/4;
79        else
80           
81            [U_smooth_sub,U_tps_sub]=tps_coeff(Coord(ind_sel,:),U(ind_sel),rho);
82            [V_smooth_sub,V_tps_sub]=tps_coeff(Coord(ind_sel,:),V(ind_sel),rho);
83            UDiff=U_smooth_sub-U(ind_sel);
84            VDiff=V_smooth_sub-V(ind_sel);
85            NormDiff=UDiff.*UDiff+VDiff.*VDiff;
86            ind_ind_sel=1:numel(ind_sel);%default
87            if exist('Threshold','var')
88            FF(ind_sel)=20*(NormDiff>Threshold);%put FF value to 20 to identify the criterium of elimmination
89            ind_ind_sel=find(FF(ind_sel)==0); % select the indices of ind_sel corresponding to the remaining vectors
90            end
91            % if no value exceeds threshold, the result is recorded
92            if isequal(numel(ind_ind_sel),numel(ind_sel))
93                U_smooth(ind_sel)=U_smooth(ind_sel)+U_smooth_sub;
94                V_smooth(ind_sel)=V_smooth(ind_sel)+V_smooth_sub;
95                NbSites(isub)=numel(ind_sel);
96                Coord_tps(1:NbSites(isub),:,isub)=Coord(ind_sel,:);
97                U_tps(1:NbSites(isub)+3,isub)=U_tps_sub;
98                V_tps(1:NbSites(isub)+3,isub)=V_tps_sub;         
99                nb_select(ind_sel)=nb_select(ind_sel)+1;
100                display('good')
101                break
102            % if too few selected vectors, increase the subrange for next iteration
103            elseif numel(ind_ind_sel)<SubDomain/4 && ~isequal( ind_sel,ind_sel_previous);
104                SubRange(:,1,isub)=SubRange(:,1,isub)-Siz'/4;
105                SubRange(:,2,isub)=SubRange(:,2,isub)+Siz'/4;
106            % else interpolation-smoothing is done again with the selected vectors
107            else
108                [U_smooth_sub,U_tps_sub]=tps_coeff(Coord(ind_sel(ind_ind_sel),:),U(ind_sel(ind_ind_sel)),rho);
109                [V_smooth_sub,V_tps_sub]=tps_coeff(Coord(ind_sel(ind_ind_sel),:),V(ind_sel(ind_ind_sel)),rho);
110                U_smooth(ind_sel(ind_ind_sel))=U_smooth(ind_sel(ind_ind_sel))+U_smooth_sub;
111                V_smooth(ind_sel(ind_ind_sel))=V_smooth(ind_sel(ind_ind_sel))+V_smooth_sub;
112                NbSites(isub)=numel(ind_ind_sel);
113                %Indices_tps(1:NbSites(isub),isub)=ind_sel(ind_ind_sel);
114                Coord_tps(1:NbSites(isub),:,isub)=Coord(ind_sel(ind_ind_sel),:);
115%                 Y_tps(1:NbSites(isub),:,isub)=Coord(ind_sel(ind_ind_sel),2);
116                U_tps(1:NbSites(isub)+3,isub)=U_tps_sub;
117                V_tps(1:NbSites(isub)+3,isub)=V_tps_sub;
118                nb_select(ind_sel(ind_ind_sel))=nb_select(ind_sel(ind_ind_sel))+1;
119                display('good2')
120                break
121            end
122        end
123    end
124end
125ind_empty=find(check_empty);
126%remove empty subdomains
127if ~isempty(ind_empty)
128    SubRange(:,:,ind_empty)=[];
129%     SubRangy(ind_empty,:)=[];
130%     Indices_tps(:,ind_empty)=[];
131    Coord_tps(:,:,ind_empty)=[];
132%     Y_tps(:,ind_empty)=[];
133    U_tps(:,ind_empty)=[];
134    V_tps(:,ind_empty)=[];
135end
136nb_select(nb_select==0)=1;%ones(size(find(nb_select==0)));
137U_smooth=U_smooth./nb_select;
138V_smooth=V_smooth./nb_select;
Note: See TracBrowser for help on using the repository browser.