source: trunk/src/transform_field/FFT2_detrend.m @ 1169

Last change on this file since 1169 was 1168, checked in by sommeria, 3 weeks ago

bugs repaired in civ TEST mode

File size: 4.6 KB
Line 
1% 'FFT2_detrend': calculate the 2D spectrum of the input scalar or image after removing the linear trend
2%------------------------------------------------------------------------
3%%%%  Use the general syntax for transform fields with a single input %%%%
4% OUTPUT:
5% DataOut:   output field structure
6%
7%INPUT:
8% DataIn:  first input field structure
9
10%=======================================================================
11% Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
12%   http://www.legi.grenoble-inp.fr
13%   Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr
14%
15%     This file is part of the toolbox UVMAT.
16%
17%     UVMAT is free software; you can redistribute it and/or modify
18%     it under the terms of the GNU General Public License as published
19%     by the Free Software Foundation; either version 2 of the license,
20%     or (at your option) any later version.
21%
22%     UVMAT is distributed in the hope that it will be useful,
23%     but WITHOUT ANY WARRANTY; without even the implied warranty of
24%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25%     GNU General Public License (see LICENSE.txt) for more details.
26%=======================================================================
27
28function DataOut=FFT2_detrend(DataIn)
29%------------------------------------------------------------------------
30DataOut=[];
31if strcmp(DataIn,'*')   
32    DataOut.InputFieldType='scalar';
33    return
34end
35%%%%%%%%%%%%%%%%%%%%%%%%%
36[CellInfo,NbDim,errormsg]=find_field_cells(DataIn);% detect the input fields
37if ~isempty(errormsg)
38    DataOut.Txt=errormsg;
39    return
40end
41DataOut.ListVarName={};
42DataOut.VarDimName={};
43for ilist=1:numel(CellInfo)
44    if NbDim(ilist)==2 && numel(CellInfo{ilist}.CoordIndex)==2 % field with structured 2D coordinates
45        %process scalar
46        ivar=CellInfo{ilist}.VarIndex_scalar(1);
47        VarName=DataIn.ListVarName{ivar};% name o the scalar field
48        z=double(DataIn.(VarName));% transform integer input to double real
49        z(isnan(z))=0;% set NaN values to 0
50        [npy,npx]=size(z);
51       
52        %process coordinates
53        CoordName=DataIn.ListVarName(CellInfo{ilist}.CoordIndex);
54        x1 = linspace(DataIn.(CoordName{2})(1),DataIn.(CoordName{2})(end),npx);
55        y1 =linspace(DataIn.(CoordName{1})(1),DataIn.(CoordName{1})(end),npy);
56        [x,y] = meshgrid(x1,y1);% 2D grid of coordinates
57       
58        % prepare the grid of wave vectors
59        delta_x = x1(2) - x1(1); delta_y = y1(2) - y1(1);
60        Nx = length(x1); Ny = length(y1);
61        Nxa = 1:Nx; Nya = 1:Ny;
62        ssx = find(Nxa<Nx/2); ssy = find(Nya<Ny/2);
63        Nxa(Nx-ssx+1) = -Nxa(ssx)+1; Nya(Ny-ssy+1) = -Nya(ssy)+1;
64        [Nxa, Ix] = sort(Nxa); [Nya, Iy] = sort(Nya);         
65        kx1 = (2*pi/delta_x/Nx)*(Nxa-1); ky1 = (2*pi/delta_y/Ny)*(Nya-1);
66        ss = find(ky1>=0); ky1 = ky1(ss);
67        [kx,ky] = meshgrid(kx1,ky1);
68        DataOut.(CoordName{2}) = kx1; DataOut.(CoordName{1}) = ky1;
69        if isfield(DataIn,'CoordUnit')
70            DataOut.CoordUnit=[DataIn.CoordUnit '^{-1}'];%unit of wave vectors
71            DataOut.ListGlobalAttribute={'CoordUnit'};
72        end
73       
74        % get the coeff to calulate the linear trend
75        coeff(1,1) = sum(sum(x.^2)); coeff(1,2) = sum(sum(x.*y)); coeff(1,3) = sum(sum(x));
76        coeff(2,1) = sum(sum(x.*y)); coeff(2,2) = sum(sum(y.^2)); coeff(2,3) = sum(sum(y));
77        coeff(3,1) = sum(sum(x)); coeff(3,2) = sum(sum(y)); coeff(3,3) = length(x1)*length(y1);
78        rhs(1) = sum(sum(x.*z)); rhs(2) = sum(sum(y.*z)); rhs(3) = sum(sum(z));
79       % lin_coeff = inv(coeff)*rhs';
80       lin_coeff = coeff\rhs';
81        lin_trend = lin_coeff(1)*x + lin_coeff(2)*y + lin_coeff(3);
82        z2 = z - lin_trend;% substract the linear trend to the input field
83       
84        spec2 = abs(fft2(z2)).^2;% get spectrum as squared of fft modulus
85        spec2 = spec2(Iy,Ix);
86        spec2 = spec2(ss,:);
87        DataOut.(VarName) = log10(spec2);% take the log10 of spectrum
88        %DataOut.(VarName) = spec2;
89%         spec_sum=sum(sum(spec2));
90%         kx_mean=sum(sum(spec2.*kx))/spec_sum;
91%         ky_mean=sum(sum(spec2.*ky))/spec_sum;
92%         theta=atand(ky_mean/kx_mean);
93%         lambda=2*pi/(sqrt(kx_mean*kx_mean+ky_mean*ky_mean));
94       
95        DataOut.ListVarName=[CoordName {VarName}];%list of variables
96        DataOut.VarDimName=[CoordName {CoordName}];%list of dimensions for variables
97        DataOut.VarAttribute{1}.Role='coord_y';
98        DataOut.VarAttribute{2}.Role='coord_x';
99        DataOut.VarAttribute{3}.Role='scalar';
100        break
101    end
102end
103 
104
105
Note: See TracBrowser for help on using the repository browser.