[476] | 1 | %'filter_tps': find the thin plate spline coefficients for interpolation-smoothing |
---|
[382] | 2 | %------------------------------------------------------------------------ |
---|
[581] | 3 | % [SubRange,NbCentres,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 |
---|
[581] | 7 | % NbCentres(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 |
---|
[581] | 10 | % Coord_tps(NbCentres,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 | |
---|
[581] | 21 | function [SubRange,NbCentres,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 | |
---|
| 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 |
---|
[582] | 48 | NbCentres=zeros(1,NbSubDomain);%number of interpolated values per subdomain, =0 by default |
---|
| 49 | %Coord_tps=zeros(NbVec,NbCoord,NbSubDomain);% default positions of the tps source= initial positions of the good vectors sorted by subdomain |
---|
| 50 | %U_tps=zeros(NbVec,NbSubDomain);%default spline |
---|
| 51 | %V_tps=zeros(NbVec,NbSubDomain);%default spline |
---|
[581] | 52 | W_tps=[];%default (2 component case) |
---|
| 53 | U_smooth=zeros(NbVec,1); % smoothed velocity U at the initial positions |
---|
| 54 | V_smooth=zeros(NbVec,1);% smoothed velocity V at the initial positions |
---|
| 55 | W_smooth=[];%default (2 component case) |
---|
| 56 | FF=zeros(NbVec,1); |
---|
| 57 | nb_select=zeros(NbVec,1); |
---|
[382] | 58 | check_empty=zeros(1,NbSubDomain); |
---|
[581] | 59 | |
---|
| 60 | %% calculate tps coeff in each subdomain |
---|
[382] | 61 | for isub=1:NbSubDomain |
---|
| 62 | SubRange(1,:,isub)=[CentreX(isub)-0.55*Siz(1) CentreX(isub)+0.55*Siz(1)]; |
---|
| 63 | SubRange(2,:,isub)=[CentreY(isub)-0.55*Siz(2) CentreY(isub)+0.55*Siz(2)]; |
---|
| 64 | ind_sel_previous=[]; |
---|
| 65 | ind_sel=0; |
---|
[581] | 66 | %increase iteratively the subdomain if it contains less than SubDomainNbVec/4 source vectors |
---|
| 67 | while numel(ind_sel)>numel(ind_sel_previous) |
---|
[382] | 68 | ind_sel_previous=ind_sel; |
---|
| 69 | 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)); |
---|
| 70 | % if no vector in the subdomain, skip the subdomain |
---|
| 71 | if isempty(ind_sel) |
---|
[581] | 72 | check_empty(isub)=1; |
---|
[382] | 73 | U_tps(1,isub)=0;%define U_tps and V_tps by default |
---|
| 74 | V_tps(1,isub)=0; |
---|
| 75 | break |
---|
| 76 | % if too few selected vectors, increase the subrange for next iteration |
---|
| 77 | elseif numel(ind_sel)<SubDomain/4 && ~isequal( ind_sel,ind_sel_previous); |
---|
[387] | 78 | SubRange(:,1,isub)=SubRange(:,1,isub)-Siz'/4; |
---|
| 79 | SubRange(:,2,isub)=SubRange(:,2,isub)+Siz'/4; |
---|
[581] | 80 | else |
---|
[382] | 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') |
---|
[581] | 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 |
---|
[382] | 90 | end |
---|
[387] | 91 | % if no value exceeds threshold, the result is recorded |
---|
[382] | 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; |
---|
[581] | 95 | NbCentres(isub)=numel(ind_sel); |
---|
| 96 | Coord_tps(1:NbCentres(isub),:,isub)=Coord(ind_sel,:); |
---|
| 97 | U_tps(1:NbCentres(isub)+3,isub)=U_tps_sub; |
---|
| 98 | V_tps(1:NbCentres(isub)+3,isub)=V_tps_sub; |
---|
[382] | 99 | nb_select(ind_sel)=nb_select(ind_sel)+1; |
---|
[387] | 100 | display('good') |
---|
[382] | 101 | break |
---|
[581] | 102 | % if too few selected vectors, increase the subrange for next iteration |
---|
[382] | 103 | elseif numel(ind_ind_sel)<SubDomain/4 && ~isequal( ind_sel,ind_sel_previous); |
---|
[387] | 104 | SubRange(:,1,isub)=SubRange(:,1,isub)-Siz'/4; |
---|
| 105 | SubRange(:,2,isub)=SubRange(:,2,isub)+Siz'/4; |
---|
[581] | 106 | % else interpolation-smoothing is done again with the selected vectors |
---|
[382] | 107 | else |
---|
[387] | 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); |
---|
[382] | 110 | U_smooth(ind_sel(ind_ind_sel))=U_smooth(ind_sel(ind_ind_sel))+U_smooth_sub; |
---|
[581] | 111 | V_smooth(ind_sel(ind_ind_sel))=V_smooth(ind_sel(ind_ind_sel))+V_smooth_sub; |
---|
| 112 | NbCentres(isub)=numel(ind_ind_sel); |
---|
| 113 | Coord_tps(1:NbCentres(isub),:,isub)=Coord(ind_sel(ind_ind_sel),:); |
---|
| 114 | U_tps(1:NbCentres(isub)+3,isub)=U_tps_sub; |
---|
| 115 | V_tps(1:NbCentres(isub)+3,isub)=V_tps_sub; |
---|
[382] | 116 | nb_select(ind_sel(ind_ind_sel))=nb_select(ind_sel(ind_ind_sel))+1; |
---|
| 117 | display('good2') |
---|
| 118 | break |
---|
| 119 | end |
---|
| 120 | end |
---|
| 121 | end |
---|
| 122 | end |
---|
[581] | 123 | |
---|
| 124 | %% remove empty subdomains |
---|
[382] | 125 | ind_empty=find(check_empty); |
---|
| 126 | if ~isempty(ind_empty) |
---|
| 127 | SubRange(:,:,ind_empty)=[]; |
---|
| 128 | Coord_tps(:,:,ind_empty)=[]; |
---|
| 129 | U_tps(:,ind_empty)=[]; |
---|
| 130 | V_tps(:,ind_empty)=[]; |
---|
| 131 | end |
---|
[581] | 132 | |
---|
| 133 | %% final adjustments |
---|
| 134 | nb_select(nb_select==0)=1; |
---|
| 135 | U_smooth=U_smooth./nb_select;% take the average at the intersection of several subdomains |
---|
[382] | 136 | V_smooth=V_smooth./nb_select; |
---|
[494] | 137 | fill=zeros(NbCoord+1,NbCoord,size(SubRange,3)); %matrix of zeros to complement the matrix Data.Civ1_Coord_tps (conveninent for file storage) |
---|
| 138 | Coord_tps=cat(1,Coord_tps,fill); |
---|
| 139 | |
---|