1 | % 'FFT': calculate and display spectrum of the field selected in the GUI get_field |
---|
2 | % GUI_input=FFT(hget_field) |
---|
3 | % |
---|
4 | % OUTPUT: |
---|
5 | % GUI_input: option for display in the GUI get_field |
---|
6 | % |
---|
7 | %INPUT: |
---|
8 | % hget_field: handles of the GUI get_field |
---|
9 | % |
---|
10 | |
---|
11 | function DataOut=signal_FFT(DataIn) |
---|
12 | % global spec x_vec |
---|
13 | % %requests for the visibility of input windows in the GUI series (activated directly by the selection in the menu ACTION) |
---|
14 | % if ~exist('hget_field','var') |
---|
15 | % GUI_input={'check_1Dplot'}; |
---|
16 | % return %exit the function |
---|
17 | % end |
---|
18 | % GUI_input=[]; |
---|
19 | % %initiation |
---|
20 | % hhget_field=guidata(hget_field); |
---|
21 | % abscissa_list=get(hhget_field.abscissa,'String'); |
---|
22 | % val=get(hhget_field.abscissa,'Value'); |
---|
23 | % val=val(1); |
---|
24 | % abscissa_name=abscissa_list{val}; |
---|
25 | % ordinate_list=get(hhget_field.ordinate,'String'); |
---|
26 | % val=get(hhget_field.ordinate,'Value'); |
---|
27 | % val=val(1); %take only the first variable in the list |
---|
28 | DataOut=DataIn; |
---|
29 | ordinate_name=DataIn.ListVarName{2}; |
---|
30 | abscissa_name=DataIn.ListVarName{1}; |
---|
31 | |
---|
32 | % get variable |
---|
33 | Var= DataIn.(ordinate_name); |
---|
34 | Coord_x= DataIn.(abscissa_name); |
---|
35 | np=size(Var); |
---|
36 | np_freq=floor(np(1)/2); |
---|
37 | dx=1;%default |
---|
38 | dfreq=1/np(1);%default frequency interval (abscissa= array index) |
---|
39 | sum_data=sum(Var,2); |
---|
40 | |
---|
41 | ind_select=find(~isinf(Coord_x)&~isnan(sum_data));%detect infinite values |
---|
42 | Coord_x=Coord_x(ind_select); |
---|
43 | Var=Var(ind_select,:); |
---|
44 | diff_x=diff(Coord_x); |
---|
45 | dx=min(diff_x); |
---|
46 | %interpolate on a regular abscissa interval if needed |
---|
47 | if (max(diff_x)-dx)> 0.001*dx || numel(ind_select)<np(1) |
---|
48 | xequ=Coord_x(1):dx:Coord_x(end);%equal time spacingdx= |
---|
49 | Var=interp1(Coord_x,Var,xequ); %interpolated func |
---|
50 | np=size(Var); |
---|
51 | end |
---|
52 | % funcinterp=interp1(time,func,timeq); %interpolated func |
---|
53 | dfreq=1/(Coord_x(end)-Coord_x(1));%frequency interval |
---|
54 | freq_max=1/(2*dx); |
---|
55 | Var=Var-ones(np(1),1)*mean(Var,1); %substract mean value |
---|
56 | fourier=fft(Var);%take fft (complex) |
---|
57 | spec=abs(fourier).*abs(fourier);% take square of the modulus |
---|
58 | spec=spec(1:np_freq,:);%keep only the first half (the other is symmetric) |
---|
59 | |
---|
60 | %plot |
---|
61 | figure(2); |
---|
62 | x_vec=linspace(dfreq,freq_max,np_freq); |
---|
63 | plot(x_vec',spec) |
---|
64 | xlabel('frequency (Hz)') |
---|
65 | ylabel('spectral intensity') |
---|
66 | grid on |
---|
67 | |
---|