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