source: trunk/src/filter_tps.m @ 1144

Last change on this file since 1144 was 1143, checked in by sommeria, 7 weeks ago

new display of false vectors

File size: 10.4 KB
RevLine 
[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
[1142]8% FF: false flags preserved from the input, or equal to 20 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
17% SubdomainSize: estimated number of data points in each subdomain
[1139]18% FieldSmooth: smoothing parameter
[382]19% Threshold: max diff accepted between smoothed and initial data
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]40function [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]43warning off
[896]44NbVec=size(Coord,1);% nbre of vectors in the field to interpolate
45NbCoord=size(Coord,2);% space dimension
[581]46MinCoord=min(Coord,[],1);%lower coordinate bounds
47MaxCoord=max(Coord,[],1);%upper coordinate bounds
[382]48Range=MaxCoord-MinCoord;
49AspectRatio=Range(2)/Range(1);
[896]50NbSubDomain=NbVec/SubDomainSize;% estimated number of subdomains
51NbSubDomainX=max(floor(sqrt(NbSubDomain/AspectRatio)),1);% estimated number of subdomains in x
52NbSubDomainY=max(floor(sqrt(NbSubDomain*AspectRatio)),1);% estimated number of subdomains in y
53NbSubDomain=NbSubDomainX*NbSubDomainY;% new estimated number of subdomains in a matrix shape partition in subdomains
[382]54Siz(1)=Range(1)/NbSubDomainX;%width of subdomains
55Siz(2)=Range(2)/NbSubDomainY;%height of subdomains
[896]56CentreX=linspace(MinCoord(1)+Siz(1)/2,MaxCoord(1)-Siz(1)/2,NbSubDomainX);% X positions of subdomain centres
57CentreY=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]59CentreX=reshape(CentreX,1,[]);% X positions of subdomain centres
[382]60CentreY=reshape(CentreY,1,[]);% Y positions of subdomain centres
[581]61
[1141]62%% smoothing parameter: CHANGED 03 May 2024 TO GET RESULTS INDEPENDENT OF SUBDOMAINSIZE
63%smoothing=Siz(1)*Siz(2)*FieldSmooth/1000%optimum smoothing increase as the area of the subdomain (division by 1000 to reach good values with the default GUI input)
[1142]64NbVecSub=NbVec/NbSubDomain;% refined estimation of the nbre of vectors per subdomain
65smoothing=sqrt(Siz(1)*Siz(2)/NbVecSub)*FieldSmooth;%optimum smoothing increase as the typical mesh size =sqrt(SizX*SizY/NbVecSub)^1/2
[1143]66
[581]67%% default output
[896]68SubRange=zeros(NbCoord,2,NbSubDomain);%initialise the boundaries of subdomains
69Coord_tps=zeros(1,NbCoord,NbSubDomain);% initialize coordinates of interpolated data
70U_tps=zeros(1,NbSubDomain);% initialize  interpolated u component
71V_tps=zeros(1,NbSubDomain);% initialize interpolated v component
72NbCentre=zeros(1,NbSubDomain);%number of interpolated field values per subdomain, =0 by default
[581]73W_tps=[];%default (2 component case)
74U_smooth=zeros(NbVec,1); % smoothed velocity U at the initial positions
75V_smooth=zeros(NbVec,1);% smoothed velocity V at the initial positions
76W_smooth=[];%default (2 component case)
77FF=zeros(NbVec,1);
78nb_select=zeros(NbVec,1);
[382]79check_empty=zeros(1,NbSubDomain);
[581]80
81%% calculate tps coeff in each subdomain
[382]82for isub=1:NbSubDomain
[896]83    SubRange(1,:,isub)=[CentreX(isub)-0.55*Siz(1) CentreX(isub)+0.55*Siz(1)];%bounds of subdomain #isub in x coordinate
84    SubRange(2,:,isub)=[CentreY(isub)-0.55*Siz(2) CentreY(isub)+0.55*Siz(2)];%bounds of subdomain #isub in y coordinate
[382]85    ind_sel_previous=[];
[896]86    ind_sel=0;%initialize set of vector indices in the subdomain
[581]87    %increase iteratively the subdomain if it contains less than SubDomainNbVec/4 source vectors
88    while numel(ind_sel)>numel(ind_sel_previous)
[1143]89        ind_sel_previous=ind_sel;% record the set of selected vector indices for next iteration
90        ind_sel= find(FF==0 & 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
[896]91        % if no vector in the subdomain  #isub, skip the subdomain
[382]92        if isempty(ind_sel)
[581]93            check_empty(isub)=1;
[1143]94            break
[896]95        % if too few selected vectors, increase the subrange for next iteration
[1137]96        elseif numel(ind_sel)<SubDomainSize/4 && ~isequal( ind_sel,ind_sel_previous)
[387]97            SubRange(:,1,isub)=SubRange(:,1,isub)-Siz'/4;
98            SubRange(:,2,isub)=SubRange(:,2,isub)+Siz'/4;
[896]99        % subdomain includes enough vectors, perform tps interpolation
[581]100        else
[1139]101            [U_smooth_sub,U_tps_sub]=tps_coeff(Coord(ind_sel,:),U(ind_sel),smoothing);
102            [V_smooth_sub,V_tps_sub]=tps_coeff(Coord(ind_sel,:),V(ind_sel),smoothing);
[896]103            UDiff=U_smooth_sub-U(ind_sel);% difference between interpolated U component and initial value
104            VDiff=V_smooth_sub-V(ind_sel);% difference between interpolated V component and initial value
105            NormDiff=UDiff.*UDiff+VDiff.*VDiff;% Square of difference norm
[382]106            ind_ind_sel=1:numel(ind_sel);%default
[772]107            if exist('Threshold','var')&&~isempty(Threshold)
[1143]108                FF(ind_sel)=2*(NormDiff>Threshold);%put FF value to 2 to identify the criterium of elimmination
109                ind_ind_sel=find(FF(ind_sel)==0); % select the indices of remaining vectors in the subset of ind_sel vectors
[382]110            end
[387]111            % if no value exceeds threshold, the result is recorded
[382]112            if isequal(numel(ind_ind_sel),numel(ind_sel))
[1137]113                x_width=(SubRange(1,2,isub)-SubRange(1,1,isub))/pi;
114                y_width=(SubRange(2,2,isub)-SubRange(2,1,isub))/pi;
115                x_dist=(Coord(ind_sel,1)-CentreX(isub))/x_width;% relative x distance to the retangle centre
116                y_dist=(Coord(ind_sel,2)-CentreY(isub))/y_width;% relative ydistance to the retangle centre
117                weight=cos(x_dist).*cos(y_dist);%weighting fct =1 at the rectangle center and 0 at edge
118                U_smooth(ind_sel)=U_smooth(ind_sel)+weight.*U_smooth_sub;
119                V_smooth(ind_sel)=V_smooth(ind_sel)+weight.*V_smooth_sub;
[651]120                NbCentre(isub)=numel(ind_sel);
121                Coord_tps(1:NbCentre(isub),:,isub)=Coord(ind_sel,:);
122                U_tps(1:NbCentre(isub)+3,isub)=U_tps_sub;
123                V_tps(1:NbCentre(isub)+3,isub)=V_tps_sub;
[1137]124                nb_select(ind_sel)=nb_select(ind_sel)+weight;
[1142]125                display(['tps done with ' num2str(numel(ind_sel)) ' vectors in subdomain # ' num2str(isub)  ' among ' num2str(NbSubDomain)])
[382]126                break
[896]127            % if too few selected vectors, increase the subrange for next iteration
[1137]128            elseif numel(ind_ind_sel)<SubDomainSize/4 && ~isequal( ind_sel,ind_sel_previous)
[387]129                SubRange(:,1,isub)=SubRange(:,1,isub)-Siz'/4;
130                SubRange(:,2,isub)=SubRange(:,2,isub)+Siz'/4;
[896]131            % else interpolation-smoothing is done again with the selected vectors
[382]132            else
[1139]133                [U_smooth_sub,U_tps_sub]=tps_coeff(Coord(ind_sel(ind_ind_sel),:),U(ind_sel(ind_ind_sel)),smoothing);
134                [V_smooth_sub,V_tps_sub]=tps_coeff(Coord(ind_sel(ind_ind_sel),:),V(ind_sel(ind_ind_sel)),smoothing);
[1137]135                x_width=(SubRange(1,2,isub)-SubRange(1,1,isub))/pi;
136                y_width=(SubRange(2,2,isub)-SubRange(2,1,isub))/pi;
137                x_dist=(Coord(ind_sel(ind_ind_sel),1)-CentreX(isub))/x_width;% relative x distance to the retangle centre
138                y_dist=(Coord(ind_sel(ind_ind_sel),2)-CentreY(isub))/y_width;% relative ydistance to the retangle centre
139                weight=cos(x_dist).*cos(y_dist);%weighting fct =1 at the rectangle center and 0 at edge
140                U_smooth(ind_sel(ind_ind_sel))=U_smooth(ind_sel(ind_ind_sel))+weight.*U_smooth_sub;
141                V_smooth(ind_sel(ind_ind_sel))=V_smooth(ind_sel(ind_ind_sel))+weight.*V_smooth_sub;
[651]142                NbCentre(isub)=numel(ind_ind_sel);
143                Coord_tps(1:NbCentre(isub),:,isub)=Coord(ind_sel(ind_ind_sel),:);
144                U_tps(1:NbCentre(isub)+3,isub)=U_tps_sub;
145                V_tps(1:NbCentre(isub)+3,isub)=V_tps_sub;
[1137]146                nb_select(ind_sel(ind_ind_sel))=nb_select(ind_sel(ind_ind_sel))+weight;
[1143]147                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]148                break
149            end
150        end
151    end
152end
[581]153
154%% remove empty subdomains
[382]155ind_empty=find(check_empty);
156if ~isempty(ind_empty)
157    SubRange(:,:,ind_empty)=[];
158    Coord_tps(:,:,ind_empty)=[];
159    U_tps(:,ind_empty)=[];
160    V_tps(:,ind_empty)=[];
[896]161    NbCentre(ind_empty)=[];
[382]162end
[581]163
164%% final adjustments
165nb_select(nb_select==0)=1;
166U_smooth=U_smooth./nb_select;% take the average at the intersection of several subdomains
[382]167V_smooth=V_smooth./nb_select;
[1143]168U_smooth(FF==2)=U(FF==2);% set to the initial values the eliminated vectors (flagged as false)
169V_smooth(FF==2)=V(FF==2);
[494]170fill=zeros(NbCoord+1,NbCoord,size(SubRange,3)); %matrix of zeros to complement the matrix Data.Civ1_Coord_tps (conveninent for file storage)
171Coord_tps=cat(1,Coord_tps,fill);
172
Note: See TracBrowser for help on using the repository browser.