[476] | 1 | %'filter_tps': find the thin plate spline coefficients for interpolation-smoothing |
---|
[382] | 2 | %------------------------------------------------------------------------ |
---|
[1139] | 3 | % [SubRange,NbCentre,Coord_tps,U_tps,V_tps,W_tps,U_smooth,V_smooth,W_smooth,FF] =filter_tps(Coord,U,V,W,SubDomainSize,FieldSmooth,Threshold) |
---|
[476] | 4 | % |
---|
[382] | 5 | % OUTPUT: |
---|
[1135] | 6 | % SubRange(NbCoord,2,NbSubdomain): range (min, max) of the coordinates x and y respectively, for each subdomain |
---|
[651] | 7 | % NbCentre(NbSubdomain): number of source points for each subdomain |
---|
[1154] | 8 | % FF: false flags preserved from the input, or equal to true for vectors excluded by the difference with the smoothed field |
---|
[382] | 9 | % U_smooth, V_smooth: filtered velocity components at the positions of the initial data |
---|
[651] | 10 | % Coord_tps(NbCentre,NbCoord,NbSubdomain): positions of the tps centres |
---|
[1142] | 11 | % U_tps,V_tps: weight of the tps centers 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 |
---|
[1135] | 16 | % U,V, possibly W: set of velocity components of the initial data |
---|
[1161] | 17 | % SubdomainSize: estimated number of data points in each subdomain, choose a number > 125 (must be >= 12 for convergence of tps after division by 4) |
---|
[1139] | 18 | % FieldSmooth: smoothing parameter |
---|
[1161] | 19 | % Threshold: max diff accepted between smoothed and initial data |
---|
[382] | 20 | |
---|
[1135] | 21 | |
---|
[809] | 22 | %======================================================================= |
---|
[1126] | 23 | % Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
---|
[809] | 24 | % http://www.legi.grenoble-inp.fr |
---|
[1127] | 25 | % Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr |
---|
[809] | 26 | % |
---|
| 27 | % This file is part of the toolbox UVMAT. |
---|
| 28 | % |
---|
| 29 | % UVMAT is free software; you can redistribute it and/or modify |
---|
| 30 | % it under the terms of the GNU General Public License as published |
---|
| 31 | % by the Free Software Foundation; either version 2 of the license, |
---|
| 32 | % or (at your option) any later version. |
---|
| 33 | % |
---|
| 34 | % UVMAT is distributed in the hope that it will be useful, |
---|
| 35 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 36 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 37 | % GNU General Public License (see LICENSE.txt) for more details. |
---|
| 38 | %======================================================================= |
---|
| 39 | |
---|
[1139] | 40 | function [SubRange,NbCentre,Coord_tps,U_tps,V_tps,W_tps,U_smooth,V_smooth,W_smooth,FF] =filter_tps(Coord,U,V,W,SubDomainSize,FieldSmooth,Threshold) |
---|
[581] | 41 | |
---|
| 42 | %% adjust subdomain decomposition |
---|
[382] | 43 | warning off |
---|
[896] | 44 | NbVec=size(Coord,1);% nbre of vectors in the field to interpolate |
---|
| 45 | NbCoord=size(Coord,2);% space dimension |
---|
[581] | 46 | MinCoord=min(Coord,[],1);%lower coordinate bounds |
---|
| 47 | MaxCoord=max(Coord,[],1);%upper coordinate bounds |
---|
[382] | 48 | Range=MaxCoord-MinCoord; |
---|
| 49 | AspectRatio=Range(2)/Range(1); |
---|
[896] | 50 | NbSubDomain=NbVec/SubDomainSize;% estimated number of subdomains |
---|
[1161] | 51 | NbSubDomainX=max(floor(sqrt(NbSubDomain/AspectRatio)),1);% estimated number of subdomains in x |
---|
[896] | 52 | NbSubDomainY=max(floor(sqrt(NbSubDomain*AspectRatio)),1);% estimated number of subdomains in y |
---|
| 53 | NbSubDomain=NbSubDomainX*NbSubDomainY;% new estimated number of subdomains in a matrix shape partition in subdomains |
---|
[382] | 54 | Siz(1)=Range(1)/NbSubDomainX;%width of subdomains |
---|
| 55 | Siz(2)=Range(2)/NbSubDomainY;%height of subdomains |
---|
[896] | 56 | CentreX=linspace(MinCoord(1)+Siz(1)/2,MaxCoord(1)-Siz(1)/2,NbSubDomainX);% X positions of subdomain centres |
---|
| 57 | CentreY=linspace(MinCoord(2)+Siz(2)/2,MaxCoord(2)-Siz(2)/2,NbSubDomainY);% Y positions of subdomain centres |
---|
[382] | 58 | [CentreX,CentreY]=meshgrid(CentreX,CentreY); |
---|
[896] | 59 | CentreX=reshape(CentreX,1,[]);% X positions of subdomain centres |
---|
[382] | 60 | CentreY=reshape(CentreY,1,[]);% Y positions of subdomain centres |
---|
[581] | 61 | |
---|
[1141] | 62 | %% smoothing parameter: CHANGED 03 May 2024 TO GET RESULTS INDEPENDENT OF SUBDOMAINSIZE |
---|
[1154] | 63 | %smoothing=Siz(1)*Siz(2)*FieldSmooth/1000%old calculation before 03 May < r1129 |
---|
[1142] | 64 | NbVecSub=NbVec/NbSubDomain;% refined estimation of the nbre of vectors per subdomain |
---|
| 65 | smoothing=sqrt(Siz(1)*Siz(2)/NbVecSub)*FieldSmooth;%optimum smoothing increase as the typical mesh size =sqrt(SizX*SizY/NbVecSub)^1/2 |
---|
[1154] | 66 | Threshold=Threshold*Threshold;% take the square of the threshold to work with the modulus squared (not done before r1154) |
---|
[1143] | 67 | |
---|
[581] | 68 | %% default output |
---|
[896] | 69 | SubRange=zeros(NbCoord,2,NbSubDomain);%initialise the boundaries of subdomains |
---|
| 70 | Coord_tps=zeros(1,NbCoord,NbSubDomain);% initialize coordinates of interpolated data |
---|
| 71 | U_tps=zeros(1,NbSubDomain);% initialize interpolated u component |
---|
| 72 | V_tps=zeros(1,NbSubDomain);% initialize interpolated v component |
---|
| 73 | NbCentre=zeros(1,NbSubDomain);%number of interpolated field values per subdomain, =0 by default |
---|
[581] | 74 | W_tps=[];%default (2 component case) |
---|
| 75 | U_smooth=zeros(NbVec,1); % smoothed velocity U at the initial positions |
---|
| 76 | V_smooth=zeros(NbVec,1);% smoothed velocity V at the initial positions |
---|
| 77 | W_smooth=[];%default (2 component case) |
---|
[1154] | 78 | FF=false(NbVec,1);%false flag=0 (false) by default |
---|
[581] | 79 | nb_select=zeros(NbVec,1); |
---|
[1161] | 80 | check_empty=false(1,NbSubDomain); |
---|
[581] | 81 | |
---|
| 82 | %% calculate tps coeff in each subdomain |
---|
[382] | 83 | for isub=1:NbSubDomain |
---|
[896] | 84 | SubRange(1,:,isub)=[CentreX(isub)-0.55*Siz(1) CentreX(isub)+0.55*Siz(1)];%bounds of subdomain #isub in x coordinate |
---|
| 85 | SubRange(2,:,isub)=[CentreY(isub)-0.55*Siz(2) CentreY(isub)+0.55*Siz(2)];%bounds of subdomain #isub in y coordinate |
---|
| 86 | ind_sel=0;%initialize set of vector indices in the subdomain |
---|
[1161] | 87 | %increase iteratively the subdomain if it contains less than |
---|
| 88 | %SubDomainNbVec/4 source vectors, until possibly cover the whole domain:check_partial_domain=false |
---|
| 89 | check_partial_domain= true; |
---|
| 90 | while check_partial_domain |
---|
| 91 | %check_next=false;% test to go to next iteration with wider subdomain |
---|
| 92 | ind_sel_previous=ind_sel;% record the set of selected vector indices for next iteration |
---|
[1154] | 93 | ind_sel= find(~FF & Coord(:,1)>=SubRange(1,1,isub) & Coord(:,1)<=SubRange(1,2,isub) & Coord(:,2)>=SubRange(2,1,isub) & Coord(:,2)<=SubRange(2,2,isub));% indices of vectors in the subdomain #isub |
---|
[1161] | 94 | check_partial_domain=sum(SubRange(:,1,isub)> MinCoord' | SubRange(:,2,isub)< MaxCoord'); |
---|
| 95 | |
---|
[896] | 96 | % if no vector in the subdomain #isub, skip the subdomain |
---|
[382] | 97 | if isempty(ind_sel) |
---|
[1161] | 98 | check_empty(isub)=true; |
---|
| 99 | break |
---|
| 100 | |
---|
| 101 | % if too few selected vectors, increase the subrange for next iteration by 50 % in each direction |
---|
| 102 | elseif numel(ind_sel)<SubDomainSize/4 && ~isequal( ind_sel,ind_sel_previous)&& check_partial_domain |
---|
| 103 | % SubRange(:,1,isub)=SubRange(:,1,isub)-Siz'/4; |
---|
| 104 | % SubRange(:,2,isub)=SubRange(:,2,isub)+Siz'/4; |
---|
| 105 | SubRange(:,1,isub)=1.25*SubRange(:,1,isub)-0.25*SubRange(:,2,isub); |
---|
| 106 | SubRange(:,2,isub)=-0.25*SubRange(:,1,isub)+1.25*SubRange(:,2,isub); |
---|
| 107 | |
---|
| 108 | % if subdomain includes enough vectors, perform tps interpolation |
---|
[581] | 109 | else |
---|
[1139] | 110 | [U_smooth_sub,U_tps_sub]=tps_coeff(Coord(ind_sel,:),U(ind_sel),smoothing); |
---|
| 111 | [V_smooth_sub,V_tps_sub]=tps_coeff(Coord(ind_sel,:),V(ind_sel),smoothing); |
---|
[896] | 112 | UDiff=U_smooth_sub-U(ind_sel);% difference between interpolated U component and initial value |
---|
| 113 | VDiff=V_smooth_sub-V(ind_sel);% difference between interpolated V component and initial value |
---|
| 114 | NormDiff=UDiff.*UDiff+VDiff.*VDiff;% Square of difference norm |
---|
[382] | 115 | ind_ind_sel=1:numel(ind_sel);%default |
---|
[772] | 116 | if exist('Threshold','var')&&~isempty(Threshold) |
---|
[1154] | 117 | FF(ind_sel)=(NormDiff>Threshold);%put FF value to 1 to identify the criterium of elimmination |
---|
[1161] | 118 | ind_ind_sel=find(~FF(ind_sel)); % select the indices of remaining vectors in the subset of ind_sel vectors |
---|
[382] | 119 | end |
---|
[387] | 120 | % if no value exceeds threshold, the result is recorded |
---|
[1161] | 121 | |
---|
[382] | 122 | if isequal(numel(ind_ind_sel),numel(ind_sel)) |
---|
[1137] | 123 | x_width=(SubRange(1,2,isub)-SubRange(1,1,isub))/pi; |
---|
| 124 | y_width=(SubRange(2,2,isub)-SubRange(2,1,isub))/pi; |
---|
| 125 | x_dist=(Coord(ind_sel,1)-CentreX(isub))/x_width;% relative x distance to the retangle centre |
---|
| 126 | y_dist=(Coord(ind_sel,2)-CentreY(isub))/y_width;% relative ydistance to the retangle centre |
---|
| 127 | weight=cos(x_dist).*cos(y_dist);%weighting fct =1 at the rectangle center and 0 at edge |
---|
[1154] | 128 | %weight=1;% case for r1129 and before |
---|
[1137] | 129 | U_smooth(ind_sel)=U_smooth(ind_sel)+weight.*U_smooth_sub; |
---|
| 130 | V_smooth(ind_sel)=V_smooth(ind_sel)+weight.*V_smooth_sub; |
---|
[651] | 131 | NbCentre(isub)=numel(ind_sel); |
---|
| 132 | Coord_tps(1:NbCentre(isub),:,isub)=Coord(ind_sel,:); |
---|
| 133 | U_tps(1:NbCentre(isub)+3,isub)=U_tps_sub; |
---|
| 134 | V_tps(1:NbCentre(isub)+3,isub)=V_tps_sub; |
---|
[1137] | 135 | nb_select(ind_sel)=nb_select(ind_sel)+weight; |
---|
[1142] | 136 | display(['tps done with ' num2str(numel(ind_sel)) ' vectors in subdomain # ' num2str(isub) ' among ' num2str(NbSubDomain)]) |
---|
[382] | 137 | break |
---|
[1161] | 138 | % if too few selected vectors, increase the subrange for next iteration by 50% in each direction |
---|
| 139 | elseif numel(ind_ind_sel)<SubDomainSize/4 && ~isequal( ind_sel,ind_sel_previous)&& check_partial_domain |
---|
| 140 | % SubRange(:,1,isub)=SubRange(:,1,isub)-Siz'/4; |
---|
| 141 | % SubRange(:,2,isub)=SubRange(:,2,isub)+Siz'/4; |
---|
| 142 | SubRange(:,1,isub)=1.25*SubRange(:,1,isub)-0.25*SubRange(:,2,isub); |
---|
| 143 | SubRange(:,2,isub)=-0.25*SubRange(:,1,isub)+1.25*SubRange(:,2,isub); |
---|
[896] | 144 | % else interpolation-smoothing is done again with the selected vectors |
---|
[382] | 145 | else |
---|
[1139] | 146 | [U_smooth_sub,U_tps_sub]=tps_coeff(Coord(ind_sel(ind_ind_sel),:),U(ind_sel(ind_ind_sel)),smoothing); |
---|
| 147 | [V_smooth_sub,V_tps_sub]=tps_coeff(Coord(ind_sel(ind_ind_sel),:),V(ind_sel(ind_ind_sel)),smoothing); |
---|
[1137] | 148 | x_width=(SubRange(1,2,isub)-SubRange(1,1,isub))/pi; |
---|
| 149 | y_width=(SubRange(2,2,isub)-SubRange(2,1,isub))/pi; |
---|
| 150 | x_dist=(Coord(ind_sel(ind_ind_sel),1)-CentreX(isub))/x_width;% relative x distance to the retangle centre |
---|
| 151 | y_dist=(Coord(ind_sel(ind_ind_sel),2)-CentreY(isub))/y_width;% relative ydistance to the retangle centre |
---|
| 152 | weight=cos(x_dist).*cos(y_dist);%weighting fct =1 at the rectangle center and 0 at edge |
---|
[1154] | 153 | %weight=1; |
---|
[1137] | 154 | U_smooth(ind_sel(ind_ind_sel))=U_smooth(ind_sel(ind_ind_sel))+weight.*U_smooth_sub; |
---|
| 155 | V_smooth(ind_sel(ind_ind_sel))=V_smooth(ind_sel(ind_ind_sel))+weight.*V_smooth_sub; |
---|
[651] | 156 | NbCentre(isub)=numel(ind_ind_sel); |
---|
| 157 | Coord_tps(1:NbCentre(isub),:,isub)=Coord(ind_sel(ind_ind_sel),:); |
---|
| 158 | U_tps(1:NbCentre(isub)+3,isub)=U_tps_sub; |
---|
| 159 | V_tps(1:NbCentre(isub)+3,isub)=V_tps_sub; |
---|
[1137] | 160 | nb_select(ind_sel(ind_ind_sel))=nb_select(ind_sel(ind_ind_sel))+weight; |
---|
[1143] | 161 | display(['tps redone with ' num2str(numel(ind_sel)) ' vectors after elimination of ' num2str(numel(ind_sel)-numel(ind_ind_sel)) ' erratic vectors in subdomain # ' num2str(isub) ' among ' num2str(NbSubDomain)]) |
---|
[382] | 162 | break |
---|
| 163 | end |
---|
| 164 | end |
---|
[1161] | 165 | % check_next=true;% go to next iteration with wider subdomain |
---|
| 166 | end% end of while loop fo increasing subdomain size |
---|
| 167 | if ~check_partial_domain && isub<NbSubDomain% if the while loop proceed to the end, the whole domain size has been covered |
---|
| 168 | check_empty(isub+1:end)=true; |
---|
| 169 | break %the whole domain has been already covered, no need for new subdomains |
---|
[382] | 170 | end |
---|
| 171 | end |
---|
[581] | 172 | |
---|
| 173 | %% remove empty subdomains |
---|
[382] | 174 | ind_empty=find(check_empty); |
---|
| 175 | if ~isempty(ind_empty) |
---|
| 176 | SubRange(:,:,ind_empty)=[]; |
---|
| 177 | Coord_tps(:,:,ind_empty)=[]; |
---|
| 178 | U_tps(:,ind_empty)=[]; |
---|
| 179 | V_tps(:,ind_empty)=[]; |
---|
[896] | 180 | NbCentre(ind_empty)=[]; |
---|
[382] | 181 | end |
---|
[581] | 182 | |
---|
| 183 | %% final adjustments |
---|
| 184 | nb_select(nb_select==0)=1; |
---|
| 185 | U_smooth=U_smooth./nb_select;% take the average at the intersection of several subdomains |
---|
[382] | 186 | V_smooth=V_smooth./nb_select; |
---|
[1154] | 187 | |
---|
| 188 | %eliminate the vectors with diff>threshold not yet eliminated |
---|
| 189 | if exist('Threshold','var')&&~isempty(Threshold) |
---|
| 190 | UDiff=U_smooth-U;% difference between interpolated U component and initial value |
---|
| 191 | VDiff=V_smooth-V;% difference between interpolated V component and initial value |
---|
| 192 | NormDiff=UDiff.*UDiff+VDiff.*VDiff;% Square of difference norm |
---|
| 193 | FF(NormDiff>Threshold)=true;%put FF value to 1 to identify the criterium of elimmination |
---|
| 194 | end |
---|
| 195 | |
---|
| 196 | U_smooth(FF)=U(FF);% set to the initial values the eliminated vectors (flagged as false) |
---|
| 197 | V_smooth(FF)=V(FF); |
---|
[494] | 198 | fill=zeros(NbCoord+1,NbCoord,size(SubRange,3)); %matrix of zeros to complement the matrix Data.Civ1_Coord_tps (conveninent for file storage) |
---|
| 199 | Coord_tps=cat(1,Coord_tps,fill); |
---|
| 200 | |
---|