[476] | 1 | %'filter_tps': find the thin plate spline coefficients for interpolation-smoothing |
---|
[382] | 2 | %------------------------------------------------------------------------ |
---|
[651] | 3 | % [SubRange,NbCentre,Coord_tps,U_tps,V_tps,W_tps,U_smooth,V_smooth,W_smooth,FF] =filter_tps(Coord,U,V,W,SubDomain,Rho,Threshold) |
---|
[476] | 4 | % |
---|
[382] | 5 | % OUTPUT: |
---|
| 6 | % SubRange(NbCoord,NbSubdomain,2): range (min, max) of the coordiantes x and y respectively, for each subdomain |
---|
[651] | 7 | % NbCentre(NbSubdomain): number of source points for each subdomain |
---|
[382] | 8 | % FF: false flags |
---|
| 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 |
---|
[382] | 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 | |
---|
[651] | 21 | function [SubRange,NbCentre,Coord_tps,U_tps,V_tps,W_tps,U_smooth,V_smooth,W_smooth,FF] =filter_tps(Coord,U,V,W,SubDomain,Rho,Threshold) |
---|
[581] | 22 | |
---|
| 23 | %% adjust subdomain decomposition |
---|
[382] | 24 | warning off |
---|
[581] | 25 | NbVec=size(Coord,1); |
---|
[382] | 26 | NbCoord=size(Coord,2); |
---|
[581] | 27 | MinCoord=min(Coord,[],1);%lower coordinate bounds |
---|
| 28 | MaxCoord=max(Coord,[],1);%upper coordinate bounds |
---|
[382] | 29 | Range=MaxCoord-MinCoord; |
---|
| 30 | AspectRatio=Range(2)/Range(1); |
---|
[581] | 31 | NbSubDomain=NbVec/SubDomain; |
---|
[382] | 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 |
---|
[581] | 42 | |
---|
| 43 | %% smoothing parameter |
---|
| 44 | rho=Siz(1)*Siz(2)*Rho/1000;%optimum rho increase as the area of the subdomain (division by 1000 to reach good values with the default GUI input) |
---|
| 45 | |
---|
| 46 | %% default output |
---|
| 47 | SubRange=zeros(NbCoord,2,NbSubDomain);%initialise the positions of subdomains |
---|
[651] | 48 | NbCentre=zeros(1,NbSubDomain);%number of interpolated values per subdomain, =0 by default |
---|
[581] | 49 | W_tps=[];%default (2 component case) |
---|
| 50 | U_smooth=zeros(NbVec,1); % smoothed velocity U at the initial positions |
---|
| 51 | V_smooth=zeros(NbVec,1);% smoothed velocity V at the initial positions |
---|
| 52 | W_smooth=[];%default (2 component case) |
---|
| 53 | FF=zeros(NbVec,1); |
---|
| 54 | nb_select=zeros(NbVec,1); |
---|
[382] | 55 | check_empty=zeros(1,NbSubDomain); |
---|
[581] | 56 | |
---|
| 57 | %% calculate tps coeff in each subdomain |
---|
[382] | 58 | for isub=1:NbSubDomain |
---|
| 59 | SubRange(1,:,isub)=[CentreX(isub)-0.55*Siz(1) CentreX(isub)+0.55*Siz(1)]; |
---|
| 60 | SubRange(2,:,isub)=[CentreY(isub)-0.55*Siz(2) CentreY(isub)+0.55*Siz(2)]; |
---|
| 61 | ind_sel_previous=[]; |
---|
| 62 | ind_sel=0; |
---|
[581] | 63 | %increase iteratively the subdomain if it contains less than SubDomainNbVec/4 source vectors |
---|
| 64 | while numel(ind_sel)>numel(ind_sel_previous) |
---|
[382] | 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) |
---|
[581] | 69 | check_empty(isub)=1; |
---|
[382] | 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); |
---|
[387] | 75 | SubRange(:,1,isub)=SubRange(:,1,isub)-Siz'/4; |
---|
| 76 | SubRange(:,2,isub)=SubRange(:,2,isub)+Siz'/4; |
---|
[581] | 77 | else |
---|
[382] | 78 | [U_smooth_sub,U_tps_sub]=tps_coeff(Coord(ind_sel,:),U(ind_sel),rho); |
---|
| 79 | [V_smooth_sub,V_tps_sub]=tps_coeff(Coord(ind_sel,:),V(ind_sel),rho); |
---|
| 80 | UDiff=U_smooth_sub-U(ind_sel); |
---|
| 81 | VDiff=V_smooth_sub-V(ind_sel); |
---|
| 82 | NormDiff=UDiff.*UDiff+VDiff.*VDiff; |
---|
| 83 | ind_ind_sel=1:numel(ind_sel);%default |
---|
| 84 | if exist('Threshold','var') |
---|
[581] | 85 | FF(ind_sel)=20*(NormDiff>Threshold);%put FF value to 20 to identify the criterium of elimmination |
---|
| 86 | ind_ind_sel=find(FF(ind_sel)==0); % select the indices of ind_sel corresponding to the remaining vectors |
---|
[382] | 87 | end |
---|
[387] | 88 | % if no value exceeds threshold, the result is recorded |
---|
[382] | 89 | if isequal(numel(ind_ind_sel),numel(ind_sel)) |
---|
| 90 | U_smooth(ind_sel)=U_smooth(ind_sel)+U_smooth_sub; |
---|
| 91 | V_smooth(ind_sel)=V_smooth(ind_sel)+V_smooth_sub; |
---|
[651] | 92 | NbCentre(isub)=numel(ind_sel); |
---|
| 93 | Coord_tps(1:NbCentre(isub),:,isub)=Coord(ind_sel,:); |
---|
| 94 | U_tps(1:NbCentre(isub)+3,isub)=U_tps_sub; |
---|
| 95 | V_tps(1:NbCentre(isub)+3,isub)=V_tps_sub; |
---|
[382] | 96 | nb_select(ind_sel)=nb_select(ind_sel)+1; |
---|
[387] | 97 | display('good') |
---|
[382] | 98 | break |
---|
[581] | 99 | % if too few selected vectors, increase the subrange for next iteration |
---|
[382] | 100 | elseif numel(ind_ind_sel)<SubDomain/4 && ~isequal( ind_sel,ind_sel_previous); |
---|
[387] | 101 | SubRange(:,1,isub)=SubRange(:,1,isub)-Siz'/4; |
---|
| 102 | SubRange(:,2,isub)=SubRange(:,2,isub)+Siz'/4; |
---|
[581] | 103 | % else interpolation-smoothing is done again with the selected vectors |
---|
[382] | 104 | else |
---|
[387] | 105 | [U_smooth_sub,U_tps_sub]=tps_coeff(Coord(ind_sel(ind_ind_sel),:),U(ind_sel(ind_ind_sel)),rho); |
---|
| 106 | [V_smooth_sub,V_tps_sub]=tps_coeff(Coord(ind_sel(ind_ind_sel),:),V(ind_sel(ind_ind_sel)),rho); |
---|
[382] | 107 | U_smooth(ind_sel(ind_ind_sel))=U_smooth(ind_sel(ind_ind_sel))+U_smooth_sub; |
---|
[581] | 108 | V_smooth(ind_sel(ind_ind_sel))=V_smooth(ind_sel(ind_ind_sel))+V_smooth_sub; |
---|
[651] | 109 | NbCentre(isub)=numel(ind_ind_sel); |
---|
| 110 | Coord_tps(1:NbCentre(isub),:,isub)=Coord(ind_sel(ind_ind_sel),:); |
---|
| 111 | U_tps(1:NbCentre(isub)+3,isub)=U_tps_sub; |
---|
| 112 | V_tps(1:NbCentre(isub)+3,isub)=V_tps_sub; |
---|
[382] | 113 | nb_select(ind_sel(ind_ind_sel))=nb_select(ind_sel(ind_ind_sel))+1; |
---|
| 114 | display('good2') |
---|
| 115 | break |
---|
| 116 | end |
---|
| 117 | end |
---|
| 118 | end |
---|
| 119 | end |
---|
[581] | 120 | |
---|
| 121 | %% remove empty subdomains |
---|
[382] | 122 | ind_empty=find(check_empty); |
---|
| 123 | if ~isempty(ind_empty) |
---|
| 124 | SubRange(:,:,ind_empty)=[]; |
---|
| 125 | Coord_tps(:,:,ind_empty)=[]; |
---|
| 126 | U_tps(:,ind_empty)=[]; |
---|
| 127 | V_tps(:,ind_empty)=[]; |
---|
| 128 | end |
---|
[581] | 129 | |
---|
| 130 | %% final adjustments |
---|
| 131 | nb_select(nb_select==0)=1; |
---|
| 132 | U_smooth=U_smooth./nb_select;% take the average at the intersection of several subdomains |
---|
[382] | 133 | V_smooth=V_smooth./nb_select; |
---|
[494] | 134 | fill=zeros(NbCoord+1,NbCoord,size(SubRange,3)); %matrix of zeros to complement the matrix Data.Civ1_Coord_tps (conveninent for file storage) |
---|
| 135 | Coord_tps=cat(1,Coord_tps,fill); |
---|
| 136 | |
---|