source: trunk/src/filter_tps.m @ 756

Last change on this file since 756 was 651, checked in by sommeria, 11 years ago

various bugs corrected. Introduction of campaign lists in Open menu

File size: 6.7 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
21function [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)
22
23%% adjust subdomain decomposition
24warning off
25NbVec=size(Coord,1);
26NbCoord=size(Coord,2);
27MinCoord=min(Coord,[],1);%lower coordinate bounds
28MaxCoord=max(Coord,[],1);%upper coordinate bounds
29Range=MaxCoord-MinCoord;
30AspectRatio=Range(2)/Range(1);
31NbSubDomain=NbVec/SubDomain;
32NbSubDomainX=max(floor(sqrt(NbSubDomain/AspectRatio)),1);
33NbSubDomainY=max(floor(sqrt(NbSubDomain*AspectRatio)),1);
34NbSubDomain=NbSubDomainX*NbSubDomainY;
35Siz(1)=Range(1)/NbSubDomainX;%width of subdomains
36Siz(2)=Range(2)/NbSubDomainY;%height of subdomains
37CentreX=linspace(MinCoord(1)+Siz(1)/2,MaxCoord(1)-Siz(1)/2,NbSubDomainX);
38CentreY=linspace(MinCoord(2)+Siz(2)/2,MaxCoord(2)-Siz(2)/2,NbSubDomainY);
39[CentreX,CentreY]=meshgrid(CentreX,CentreY);
40CentreY=reshape(CentreY,1,[]);% Y positions of subdomain centres
41CentreX=reshape(CentreX,1,[]);% X positions of subdomain centres
42
43%% smoothing parameter
44rho=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
47SubRange=zeros(NbCoord,2,NbSubDomain);%initialise the positions of subdomains
48NbCentre=zeros(1,NbSubDomain);%number of interpolated values per subdomain, =0 by default
49W_tps=[];%default (2 component case)
50U_smooth=zeros(NbVec,1); % smoothed velocity U at the initial positions
51V_smooth=zeros(NbVec,1);% smoothed velocity V at the initial positions
52W_smooth=[];%default (2 component case)
53FF=zeros(NbVec,1);
54nb_select=zeros(NbVec,1);
55check_empty=zeros(1,NbSubDomain);
56
57%% calculate tps coeff in each subdomain
58for 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;
63    %increase iteratively the subdomain if it contains less than SubDomainNbVec/4 source vectors
64    while numel(ind_sel)>numel(ind_sel_previous)
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)
69            check_empty(isub)=1;
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);
75            SubRange(:,1,isub)=SubRange(:,1,isub)-Siz'/4;
76            SubRange(:,2,isub)=SubRange(:,2,isub)+Siz'/4;
77        else
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')
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
87            end
88            % if no value exceeds threshold, the result is recorded
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;
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;
96                nb_select(ind_sel)=nb_select(ind_sel)+1;
97                display('good')
98                break
99                % if too few selected vectors, increase the subrange for next iteration
100            elseif numel(ind_ind_sel)<SubDomain/4 && ~isequal( ind_sel,ind_sel_previous);
101                SubRange(:,1,isub)=SubRange(:,1,isub)-Siz'/4;
102                SubRange(:,2,isub)=SubRange(:,2,isub)+Siz'/4;
103                % else interpolation-smoothing is done again with the selected vectors
104            else
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);
107                U_smooth(ind_sel(ind_ind_sel))=U_smooth(ind_sel(ind_ind_sel))+U_smooth_sub;
108                V_smooth(ind_sel(ind_ind_sel))=V_smooth(ind_sel(ind_ind_sel))+V_smooth_sub;
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;
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
119end
120
121%% remove empty subdomains
122ind_empty=find(check_empty);
123if ~isempty(ind_empty)
124    SubRange(:,:,ind_empty)=[];
125    Coord_tps(:,:,ind_empty)=[];
126    U_tps(:,ind_empty)=[];
127    V_tps(:,ind_empty)=[];
128end
129
130%% final adjustments
131nb_select(nb_select==0)=1;
132U_smooth=U_smooth./nb_select;% take the average at the intersection of several subdomains
133V_smooth=V_smooth./nb_select;
134fill=zeros(NbCoord+1,NbCoord,size(SubRange,3)); %matrix of zeros to complement the matrix Data.Civ1_Coord_tps (conveninent for file storage)
135Coord_tps=cat(1,Coord_tps,fill);
136
Note: See TracBrowser for help on using the repository browser.