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 | |
---|
20 | function [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 |
---|
22 | warning off |
---|
23 | nbvec=size(Coord,1); |
---|
24 | W_tps=[];%default |
---|
25 | W_smooth=[]; |
---|
26 | NbCoord=size(Coord,2); |
---|
27 | NbSubDomain=nbvec/SubDomain; |
---|
28 | MinCoord=min(Coord,[],1); |
---|
29 | MaxCoord=max(Coord,[],1); |
---|
30 | Range=MaxCoord-MinCoord; |
---|
31 | AspectRatio=Range(2)/Range(1); |
---|
32 | NbSubDomainX=max(floor(sqrt(NbSubDomain/AspectRatio)),1); |
---|
33 | NbSubDomainY=max(floor(sqrt(NbSubDomain*AspectRatio)),1); |
---|
34 | NbSubDomain=NbSubDomainX*NbSubDomainY; |
---|
35 | Siz(1)=Range(1)/NbSubDomainX;%width of subdomains |
---|
36 | Siz(2)=Range(2)/NbSubDomainY;%height of subdomains |
---|
37 | CentreX=linspace(MinCoord(1)+Siz(1)/2,MaxCoord(1)-Siz(1)/2,NbSubDomainX); |
---|
38 | CentreY=linspace(MinCoord(2)+Siz(2)/2,MaxCoord(2)-Siz(2)/2,NbSubDomainY); |
---|
39 | [CentreX,CentreY]=meshgrid(CentreX,CentreY); |
---|
40 | CentreY=reshape(CentreY,1,[]);% Y positions of subdomain centres |
---|
41 | CentreX=reshape(CentreX,1,[]);% X positions of subdomain centres |
---|
42 | rho=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) |
---|
43 | U_tps_sub=zeros(nbvec,NbSubDomain);%default spline |
---|
44 | V_tps_sub=zeros(nbvec,NbSubDomain);%default spline |
---|
45 | Indices_tps=zeros(nbvec,NbSubDomain);%default indices |
---|
46 | U_smooth=zeros(nbvec,1); |
---|
47 | V_smooth=zeros(nbvec,1); |
---|
48 | nb_select=zeros(nbvec,1); |
---|
49 | FF=zeros(nbvec,1); |
---|
50 | check_empty=zeros(1,NbSubDomain); |
---|
51 | SubRange=zeros(NbCoord,2,NbSubDomain);%initialise the positions of subdomains |
---|
52 | % SubRangy=zeros(NbSubDomain,2); |
---|
53 | for 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 |
---|
113 | end |
---|
114 | ind_empty=find(check_empty); |
---|
115 | %remove empty subdomains |
---|
116 | if ~isempty(ind_empty) |
---|
117 | SubRange(:,:,ind_empty)=[]; |
---|
118 | Coord_tps(:,:,ind_empty)=[]; |
---|
119 | U_tps(:,ind_empty)=[]; |
---|
120 | V_tps(:,ind_empty)=[]; |
---|
121 | end |
---|
122 | nb_select(nb_select==0)=1;%ones(size(find(nb_select==0))); |
---|
123 | U_smooth=U_smooth./nb_select; |
---|
124 | V_smooth=V_smooth./nb_select; |
---|