source: trunk/src/filter_tps.m @ 1062

Last change on this file since 1062 was 1061, checked in by g7moreau, 6 years ago
  • Update copyright to year 2019
File size: 8.9 KB
RevLine 
[476]1%'filter_tps': find the thin plate spline coefficients for interpolation-smoothing
[382]2%------------------------------------------------------------------------
[896]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,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
[809]21%=======================================================================
[1061]22% Copyright 2008-2019, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
[809]23%   http://www.legi.grenoble-inp.fr
24%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
25%
26%     This file is part of the toolbox UVMAT.
27%
28%     UVMAT is free software; you can redistribute it and/or modify
29%     it under the terms of the GNU General Public License as published
30%     by the Free Software Foundation; either version 2 of the license,
31%     or (at your option) any later version.
32%
33%     UVMAT is distributed in the hope that it will be useful,
34%     but WITHOUT ANY WARRANTY; without even the implied warranty of
35%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36%     GNU General Public License (see LICENSE.txt) for more details.
37%=======================================================================
38
[896]39function [SubRange,NbCentre,Coord_tps,U_tps,V_tps,W_tps,U_smooth,V_smooth,W_smooth,FF] =filter_tps(Coord,U,V,W,SubDomainSize,Rho,Threshold)
[581]40
41%% adjust subdomain decomposition
[382]42warning off
[896]43NbVec=size(Coord,1);% nbre of vectors in the field to interpolate
44NbCoord=size(Coord,2);% space dimension
[581]45MinCoord=min(Coord,[],1);%lower coordinate bounds
46MaxCoord=max(Coord,[],1);%upper coordinate bounds
[382]47Range=MaxCoord-MinCoord;
48AspectRatio=Range(2)/Range(1);
[896]49NbSubDomain=NbVec/SubDomainSize;% estimated number of subdomains
50NbSubDomainX=max(floor(sqrt(NbSubDomain/AspectRatio)),1);% estimated number of subdomains in x
51NbSubDomainY=max(floor(sqrt(NbSubDomain*AspectRatio)),1);% estimated number of subdomains in y
52NbSubDomain=NbSubDomainX*NbSubDomainY;% new estimated number of subdomains in a matrix shape partition in subdomains
[382]53Siz(1)=Range(1)/NbSubDomainX;%width of subdomains
54Siz(2)=Range(2)/NbSubDomainY;%height of subdomains
[896]55CentreX=linspace(MinCoord(1)+Siz(1)/2,MaxCoord(1)-Siz(1)/2,NbSubDomainX);% X positions of subdomain centres
56CentreY=linspace(MinCoord(2)+Siz(2)/2,MaxCoord(2)-Siz(2)/2,NbSubDomainY);% Y positions of subdomain centres
[382]57[CentreX,CentreY]=meshgrid(CentreX,CentreY);
[896]58CentreX=reshape(CentreX,1,[]);% X positions of subdomain centres
[382]59CentreY=reshape(CentreY,1,[]);% Y positions of subdomain centres
[581]60
61%% smoothing parameter
62rho=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)
63
64%% default output
[896]65SubRange=zeros(NbCoord,2,NbSubDomain);%initialise the boundaries of subdomains
66Coord_tps=zeros(1,NbCoord,NbSubDomain);% initialize coordinates of interpolated data
67U_tps=zeros(1,NbSubDomain);% initialize  interpolated u component
68V_tps=zeros(1,NbSubDomain);% initialize interpolated v component
69NbCentre=zeros(1,NbSubDomain);%number of interpolated field values per subdomain, =0 by default
[581]70W_tps=[];%default (2 component case)
71U_smooth=zeros(NbVec,1); % smoothed velocity U at the initial positions
72V_smooth=zeros(NbVec,1);% smoothed velocity V at the initial positions
73W_smooth=[];%default (2 component case)
74FF=zeros(NbVec,1);
75nb_select=zeros(NbVec,1);
[382]76check_empty=zeros(1,NbSubDomain);
[581]77
[896]78
[581]79%% calculate tps coeff in each subdomain
[382]80for isub=1:NbSubDomain
[1059]81    isub
[896]82    SubRange(1,:,isub)=[CentreX(isub)-0.55*Siz(1) CentreX(isub)+0.55*Siz(1)];%bounds of subdomain #isub in x coordinate
83    SubRange(2,:,isub)=[CentreY(isub)-0.55*Siz(2) CentreY(isub)+0.55*Siz(2)];%bounds of subdomain #isub in y coordinate
[382]84    ind_sel_previous=[];
[896]85    ind_sel=0;%initialize set of vector indices in the subdomain
[581]86    %increase iteratively the subdomain if it contains less than SubDomainNbVec/4 source vectors
87    while numel(ind_sel)>numel(ind_sel_previous)
[896]88        ind_sel_previous=ind_sel;% record the set of selected vector indices for next iteration
[382]89        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));
[1059]90        numel(ind_sel)
[896]91        % if no vector in the subdomain  #isub, skip the subdomain
[382]92        if isempty(ind_sel)
[581]93            check_empty(isub)=1;
[896]94            break %  go to next subdomain
95        % if too few selected vectors, increase the subrange for next iteration
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
[382]101            [U_smooth_sub,U_tps_sub]=tps_coeff(Coord(ind_sel,:),U(ind_sel),rho);
102            [V_smooth_sub,V_tps_sub]=tps_coeff(Coord(ind_sel,:),V(ind_sel),rho);
[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)
[581]108                FF(ind_sel)=20*(NormDiff>Threshold);%put FF value to 20 to identify the criterium of elimmination
109                ind_ind_sel=find(FF(ind_sel)==0); % select the indices of ind_sel corresponding to the remaining 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))
113                U_smooth(ind_sel)=U_smooth(ind_sel)+U_smooth_sub;
114                V_smooth(ind_sel)=V_smooth(ind_sel)+V_smooth_sub;
[651]115                NbCentre(isub)=numel(ind_sel);
116                Coord_tps(1:NbCentre(isub),:,isub)=Coord(ind_sel,:);
117                U_tps(1:NbCentre(isub)+3,isub)=U_tps_sub;
118                V_tps(1:NbCentre(isub)+3,isub)=V_tps_sub;
[382]119                nb_select(ind_sel)=nb_select(ind_sel)+1;
[896]120                display(['tps done in subdomain # ' num2str(isub)  ' among ' num2str(NbSubDomain)])
[382]121                break
[896]122            % if too few selected vectors, increase the subrange for next iteration
123            elseif numel(ind_ind_sel)<SubDomainSize/4 && ~isequal( ind_sel,ind_sel_previous);
[387]124                SubRange(:,1,isub)=SubRange(:,1,isub)-Siz'/4;
125                SubRange(:,2,isub)=SubRange(:,2,isub)+Siz'/4;
[896]126            % else interpolation-smoothing is done again with the selected vectors
[382]127            else
[387]128                [U_smooth_sub,U_tps_sub]=tps_coeff(Coord(ind_sel(ind_ind_sel),:),U(ind_sel(ind_ind_sel)),rho);
129                [V_smooth_sub,V_tps_sub]=tps_coeff(Coord(ind_sel(ind_ind_sel),:),V(ind_sel(ind_ind_sel)),rho);
[382]130                U_smooth(ind_sel(ind_ind_sel))=U_smooth(ind_sel(ind_ind_sel))+U_smooth_sub;
[581]131                V_smooth(ind_sel(ind_ind_sel))=V_smooth(ind_sel(ind_ind_sel))+V_smooth_sub;
[651]132                NbCentre(isub)=numel(ind_ind_sel);
133                Coord_tps(1:NbCentre(isub),:,isub)=Coord(ind_sel(ind_ind_sel),:);
134                U_tps(1:NbCentre(isub)+3,isub)=U_tps_sub;
135                V_tps(1:NbCentre(isub)+3,isub)=V_tps_sub;
[382]136                nb_select(ind_sel(ind_ind_sel))=nb_select(ind_sel(ind_ind_sel))+1;
[896]137                display(['tps redone after elimination of erratic vectors in subdomain # ' num2str(isub) ' among ' num2str(NbSubDomain)])
[382]138                break
139            end
140        end
141    end
142end
[581]143
144%% remove empty subdomains
[382]145ind_empty=find(check_empty);
146if ~isempty(ind_empty)
147    SubRange(:,:,ind_empty)=[];
148    Coord_tps(:,:,ind_empty)=[];
149    U_tps(:,ind_empty)=[];
150    V_tps(:,ind_empty)=[];
[896]151    NbCentre(ind_empty)=[];
[382]152end
[581]153
154%% final adjustments
155nb_select(nb_select==0)=1;
156U_smooth=U_smooth./nb_select;% take the average at the intersection of several subdomains
[382]157V_smooth=V_smooth./nb_select;
[949]158U_smooth(FF==20)=U(FF==20);% set to the initial values the eliminated vectors (flagged as false)
159V_smooth(FF==20)=V(FF==20);
[494]160fill=zeros(NbCoord+1,NbCoord,size(SubRange,3)); %matrix of zeros to complement the matrix Data.Civ1_Coord_tps (conveninent for file storage)
161Coord_tps=cat(1,Coord_tps,fill);
162
Note: See TracBrowser for help on using the repository browser.