source: trunk/src/filter_tps.m @ 849

Last change on this file since 849 was 809, checked in by g7moreau, 7 years ago
  • Add license
File size: 7.6 KB
Line 
1%'filter_tps': find the thin plate spline coefficients for interpolation-smoothing
2%------------------------------------------------------------------------
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)
4%
5% OUTPUT:
6% SubRange(NbCoord,NbSubdomain,2): range (min, max) of the coordiantes x and y respectively, for each subdomain
7% NbCentre(NbSubdomain): number of source points for each subdomain
8% FF: false flags
9% U_smooth, V_smooth: filtered velocity components at the positions of the initial data
10% Coord_tps(NbCentre,NbCoord,NbSubdomain): positions of the tps centres
11% U_tps,V_tps: weight of the tps for each subdomain
12% to get the interpolated field values, use the function calc_field.m
13%
14% INPUT:
15% coord=[X Y]: matrix whose first column is the x coordinates of the initial data, the second column the y coordiantes
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
21%=======================================================================
22% Copyright 2008-2014, LEGI UMR 5519 / CNRS UJF G-INP, Grenoble, France
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
39function [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)
40
41%% adjust subdomain decomposition
42warning off
43NbVec=size(Coord,1);
44NbCoord=size(Coord,2);
45MinCoord=min(Coord,[],1);%lower coordinate bounds
46MaxCoord=max(Coord,[],1);%upper coordinate bounds
47Range=MaxCoord-MinCoord;
48AspectRatio=Range(2)/Range(1);
49NbSubDomain=NbVec/SubDomain;
50NbSubDomainX=max(floor(sqrt(NbSubDomain/AspectRatio)),1);
51NbSubDomainY=max(floor(sqrt(NbSubDomain*AspectRatio)),1);
52NbSubDomain=NbSubDomainX*NbSubDomainY;
53Siz(1)=Range(1)/NbSubDomainX;%width of subdomains
54Siz(2)=Range(2)/NbSubDomainY;%height of subdomains
55CentreX=linspace(MinCoord(1)+Siz(1)/2,MaxCoord(1)-Siz(1)/2,NbSubDomainX);
56CentreY=linspace(MinCoord(2)+Siz(2)/2,MaxCoord(2)-Siz(2)/2,NbSubDomainY);
57[CentreX,CentreY]=meshgrid(CentreX,CentreY);
58CentreY=reshape(CentreY,1,[]);% Y positions of subdomain centres
59CentreX=reshape(CentreX,1,[]);% X positions of subdomain centres
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
65SubRange=zeros(NbCoord,2,NbSubDomain);%initialise the positions of subdomains
66NbCentre=zeros(1,NbSubDomain);%number of interpolated values per subdomain, =0 by default
67W_tps=[];%default (2 component case)
68U_smooth=zeros(NbVec,1); % smoothed velocity U at the initial positions
69V_smooth=zeros(NbVec,1);% smoothed velocity V at the initial positions
70W_smooth=[];%default (2 component case)
71FF=zeros(NbVec,1);
72nb_select=zeros(NbVec,1);
73check_empty=zeros(1,NbSubDomain);
74
75%% calculate tps coeff in each subdomain
76for isub=1:NbSubDomain
77    SubRange(1,:,isub)=[CentreX(isub)-0.55*Siz(1) CentreX(isub)+0.55*Siz(1)];
78    SubRange(2,:,isub)=[CentreY(isub)-0.55*Siz(2) CentreY(isub)+0.55*Siz(2)];
79    ind_sel_previous=[];
80    ind_sel=0;
81    %increase iteratively the subdomain if it contains less than SubDomainNbVec/4 source vectors
82    while numel(ind_sel)>numel(ind_sel_previous)
83        ind_sel_previous=ind_sel;
84        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));
85        % if no vector in the subdomain, skip the subdomain
86        if isempty(ind_sel)
87            check_empty(isub)=1;
88            U_tps(1,isub)=0;%define U_tps and V_tps by default
89            V_tps(1,isub)=0;
90            break
91            % if too few selected vectors, increase the subrange for next iteration
92        elseif numel(ind_sel)<SubDomain/4 && ~isequal( ind_sel,ind_sel_previous);
93            SubRange(:,1,isub)=SubRange(:,1,isub)-Siz'/4;
94            SubRange(:,2,isub)=SubRange(:,2,isub)+Siz'/4;
95        else
96            [U_smooth_sub,U_tps_sub]=tps_coeff(Coord(ind_sel,:),U(ind_sel),rho);
97            [V_smooth_sub,V_tps_sub]=tps_coeff(Coord(ind_sel,:),V(ind_sel),rho);
98            UDiff=U_smooth_sub-U(ind_sel);
99            VDiff=V_smooth_sub-V(ind_sel);
100            NormDiff=UDiff.*UDiff+VDiff.*VDiff;
101            ind_ind_sel=1:numel(ind_sel);%default
102            if exist('Threshold','var')&&~isempty(Threshold)
103                FF(ind_sel)=20*(NormDiff>Threshold);%put FF value to 20 to identify the criterium of elimmination
104                ind_ind_sel=find(FF(ind_sel)==0); % select the indices of ind_sel corresponding to the remaining vectors
105            end
106            % if no value exceeds threshold, the result is recorded
107            if isequal(numel(ind_ind_sel),numel(ind_sel))
108                U_smooth(ind_sel)=U_smooth(ind_sel)+U_smooth_sub;
109                V_smooth(ind_sel)=V_smooth(ind_sel)+V_smooth_sub;
110                NbCentre(isub)=numel(ind_sel);
111                Coord_tps(1:NbCentre(isub),:,isub)=Coord(ind_sel,:);
112                U_tps(1:NbCentre(isub)+3,isub)=U_tps_sub;
113                V_tps(1:NbCentre(isub)+3,isub)=V_tps_sub;
114                nb_select(ind_sel)=nb_select(ind_sel)+1;
115                display('good')
116                break
117                % if too few selected vectors, increase the subrange for next iteration
118            elseif numel(ind_ind_sel)<SubDomain/4 && ~isequal( ind_sel,ind_sel_previous);
119                SubRange(:,1,isub)=SubRange(:,1,isub)-Siz'/4;
120                SubRange(:,2,isub)=SubRange(:,2,isub)+Siz'/4;
121                % else interpolation-smoothing is done again with the selected vectors
122            else
123                [U_smooth_sub,U_tps_sub]=tps_coeff(Coord(ind_sel(ind_ind_sel),:),U(ind_sel(ind_ind_sel)),rho);
124                [V_smooth_sub,V_tps_sub]=tps_coeff(Coord(ind_sel(ind_ind_sel),:),V(ind_sel(ind_ind_sel)),rho);
125                U_smooth(ind_sel(ind_ind_sel))=U_smooth(ind_sel(ind_ind_sel))+U_smooth_sub;
126                V_smooth(ind_sel(ind_ind_sel))=V_smooth(ind_sel(ind_ind_sel))+V_smooth_sub;
127                NbCentre(isub)=numel(ind_ind_sel);
128                Coord_tps(1:NbCentre(isub),:,isub)=Coord(ind_sel(ind_ind_sel),:);
129                U_tps(1:NbCentre(isub)+3,isub)=U_tps_sub;
130                V_tps(1:NbCentre(isub)+3,isub)=V_tps_sub;
131                nb_select(ind_sel(ind_ind_sel))=nb_select(ind_sel(ind_ind_sel))+1;
132                display('good2')
133                break
134            end
135        end
136    end
137end
138
139%% remove empty subdomains
140ind_empty=find(check_empty);
141if ~isempty(ind_empty)
142    SubRange(:,:,ind_empty)=[];
143    Coord_tps(:,:,ind_empty)=[];
144    U_tps(:,ind_empty)=[];
145    V_tps(:,ind_empty)=[];
146end
147
148%% final adjustments
149nb_select(nb_select==0)=1;
150U_smooth=U_smooth./nb_select;% take the average at the intersection of several subdomains
151V_smooth=V_smooth./nb_select;
152fill=zeros(NbCoord+1,NbCoord,size(SubRange,3)); %matrix of zeros to complement the matrix Data.Civ1_Coord_tps (conveninent for file storage)
153Coord_tps=cat(1,Coord_tps,fill);
154
Note: See TracBrowser for help on using the repository browser.