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 GUI_input=FFT(hget_field) |
---|
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 | |
---|
29 | ordinate_name=ordinate_list{val}; |
---|
30 | |
---|
31 | [Field,errormsg]=read_get_field(hget_field); |
---|
32 | if ~isempty(errormsg) |
---|
33 | msgbox_uvmat('ERROR',['error in get_field/FFT input:' errormsg]) |
---|
34 | return |
---|
35 | end |
---|
36 | |
---|
37 | % get variable |
---|
38 | eval(['Var= Field.' ordinate_name ';']); |
---|
39 | np=size(Var); |
---|
40 | np_freq=floor(np(1)/2); |
---|
41 | dx=1;%default |
---|
42 | dfreq=1/np(1);%default frequency interval (abscissa= array index) |
---|
43 | sum_data=sum(Var,2); |
---|
44 | if ~isequal(abscissa_name,'') |
---|
45 | eval(['Coord_x= Field.' abscissa_name ';']); |
---|
46 | ind_select=find(~isinf(Coord_x)&~isnan(sum_data));%detect infinite values |
---|
47 | Coord_x=Coord_x(ind_select); |
---|
48 | Var=Var(ind_select,:); |
---|
49 | diff_x=diff(Coord_x); |
---|
50 | dx=min(diff_x); |
---|
51 | %interpolate on a regular abscissa interval if needed |
---|
52 | if (max(diff_x)-dx)> 0.001*dx || numel(ind_select)<np(1) |
---|
53 | xequ=Coord_x(1):dx:Coord_x(end);%equal time spacingdx= |
---|
54 | Var=interp1(Coord_x,Var,xequ); %interpolated func |
---|
55 | np=size(Var); |
---|
56 | end |
---|
57 | % funcinterp=interp1(time,func,timeq); %interpolated func |
---|
58 | dfreq=1/(Coord_x(end)-Coord_x(1));%frequency interval |
---|
59 | end |
---|
60 | freq_max=1/(2*dx); |
---|
61 | Var=Var-ones(np(1),1)*mean(Var,1); %substract mean value |
---|
62 | fourier=fft(Var);%take fft (complex) |
---|
63 | spec=abs(fourier).*abs(fourier);% take square of the modulus |
---|
64 | spec=spec(1:np_freq,:);%keep only the first half (the other is symmetric) |
---|
65 | |
---|
66 | %plot |
---|
67 | list_fig=get(hhget_field.list_fig,'String'); |
---|
68 | val=get(hhget_field.list_fig,'Value'); |
---|
69 | hfig=str2num(list_fig{val});% chosen figure number from tyhe GUI |
---|
70 | if isempty(hfig) |
---|
71 | hfig=figure; |
---|
72 | else |
---|
73 | figure(hfig); |
---|
74 | end |
---|
75 | haxes=findobj(hfig,'Type','axes'); |
---|
76 | if ~isempty(haxes) |
---|
77 | axes(haxes) |
---|
78 | end |
---|
79 | x_vec=linspace(dfreq,freq_max,np_freq); |
---|
80 | plot(x_vec',spec) |
---|
81 | xlabel('frequency (Hz)') |
---|
82 | ylabel('spectral intensity') |
---|
83 | grid on |
---|
84 | |
---|
85 | % |
---|
86 | % |
---|
87 | % np=length(funcinterp); |
---|
88 | % funcinterp=funcinterp-sum(funcinterp)/np; %substract mean |
---|
89 | % fourier=fft(funcinterp);%take fft (complex) |
---|
90 | % spec=abs(fourier).*abs(fourier);% take sqare of the modulus |
---|
91 | % spec=spec([1:floor(np/2)]);%keep only the first half (the other is symmetric) |
---|
92 | % eval(['Field.' varname '=spec;']) |
---|
93 | % Field |
---|
94 | % % dfreq=1/(time(end)-time(1));%frequency interval |
---|
95 | % % freq=[0:dfreq:(floor(np/2)-1)*dfreq]; |
---|
96 | % % figure(1) |
---|
97 | % % hold on |
---|
98 | % % plot(freq,spec) |
---|
99 | % % xlabel('frequency (Hz)') |
---|
100 | % % ylabel('spectral intensity') |
---|
101 | % % title(['spectrum of' fields]); |
---|
102 | % % grid on |
---|
103 | % |
---|