-
Notifications
You must be signed in to change notification settings - Fork 1
/
fft_avt.m
165 lines (139 loc) · 4.62 KB
/
fft_avt.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
%Function for plot FFT from REC_DATA module
%Require octave, octave-signal
%dirname `rpm -ql octave-signal | grep hann.m` >> ~/.octaverc
%Prepare data
%system ('grep "3:" minicom.cap | cut -d ":" -f2 | cut -d " " -f1,2 > f3');
%grep "^0:" minicom.cap | cut -d ":" -f2 | cut -d " " -f1 > f8
%grep "^16:" minicom.cap | cut -d ":" -f2 | cut -d " " -f1,2 > f3
%histc(a,[-3 -2 -1 0 1 2 3])/length(a)*100
function [spectre_freq_points,spectre_data,plot_color] = fft_avt (data_file, Fs, smpl, use_window, data_mode, plot_color, test_mode)
if nargin < 7
test_mode = 0;
end
if nargin < 6
plot_color = 'b';
end
if nargin < 5
data_mode = 0;
end
if nargin < 4
use_window = 1;
end
if nargin < 3
error('Not enougth arguments');
end
%Check right data_mode
if data_mode ~= 0 && data_mode ~= 1 && data_mode ~= 2
error('data_mode must be 0 or 1 or 2');
end
%Check if we in Octave or Matlab and load additional library for Octave
vers = version;
if (str2num(vers(1)) < 7)
pkg load signal
end
%Set frequency threshold for fast real input, MHz
%If Fs less then FS_FAST it will be slow real or complex
FS_FAST = 150;
%================================================
%Variables for change
%Input data filename or array
%data_file = 'f0';
%data_file = f0_array;
%data must be in one or two (for complex mode) columns with " " (witespace) delimiter.
%In Test mode enter 0.
%Sampling frequency, MHz
%Fs = 190;
%Number of FFT points as power of 2
%Low number - low Frequency resolution but more smoothnes of picture. Useful 9 to 14.
%smpl = 14;
%Enable window for FFT
%0 - no window, 1 - Hann, 2 - Blackman
%use_window = 1;
%Take data from data_file in auto mode or only one column in real mode
%Good values are 0 for auto, 1 for first column, 2 for second column
%data_mode = 0
%Color of FFT plot
%plot_color = 'b';
%Test mode
%0 - normal mode, 1 - real test mode, 2 - complex test mode
%test_mode = 0;
%================================================
if test_mode == 1
f = 1:2^20;
ssin = sin(f) + 0.1*cos(f/2);
snoi = normrnd(0,0.1,2^20,1);
input_data = ssin' + snoi;
[nr,nc] = size(input_data);
cplx_mode = 'real';
elseif test_mode == 2
f = 1:2^20;
ssin0 = (sin(f) + 0.1*cos(f/4));
ssin1 = (cos(f) + 0.1*sin(f/4));
snoi0 = normrnd(0,0.1,2^20,1);
snoi1 = normrnd(0,0.1,2^20,1);
input_data0 = [ssin0;ssin1]' + [snoi0 snoi1];
input_data = complex(input_data0(:,1),input_data0(:,2));
[nr,nc] = size(input_data0)
cplx_mode = 'complex';
else
if ischar(data_file)
f_pre = load(data_file);
else
f_pre = data_file;
end
[nr,nc] = size(f_pre);
if data_mode == 0
if nc == 1
input_data = f_pre;
cplx_mode = 'real';
else
input_data = complex(f_pre(:,1),f_pre(:,2));
cplx_mode = 'complex';
end
else
input_data = f_pre(:,data_mode);
cplx_mode = 'real';
end
end
input_data_lth = length(input_data);
fft_samples = 2^smpl;
fft_samples_half = fft_samples/2;
number_dia = floor(input_data_lth/fft_samples);
Fs2 = Fs/2;
fprintf('Total %d %s samples (FFT %d samples and %d full diapasons)\n',input_data_lth,cplx_mode,fft_samples,number_dia);
fprintf('FFT precision: %d Hz (smpl = %d); MAX %d Hz (smpl = %d)\n',Fs*1e6/fft_samples,smpl,Fs*1e6/input_data_lth,log2(input_data_lth));
%Use Hann window or not
if use_window == 1
wind_coeffs = hann(fft_samples);
elseif use_window == 2
wind_coeffs = blackman(fft_samples);
else
wind_coeffs = ones(fft_samples,1);
end
%Compute separate FFT for each part
for k=1:number_dia
fft_data_parts(k,:) = abs(fft(wind_coeffs.*input_data(1+fft_samples*(k-1):fft_samples*k)));
end
%Sum samples from each part and make logarithm
fft_data_sum = sum(fft_data_parts,1);
fft_data_sum_log = 20*log10(fft_data_sum/max(fft_data_sum));
%Plot graphs
if Fs > FS_FAST
spectre_freq_points = (1:fft_samples_half)/fft_samples_half*Fs2+Fs2;
spectre_data = fft_data_sum_log(fft_samples_half+1:end);
else
if strcmp(cplx_mode,'real')
spectre_freq_points = (1:fft_samples_half)/fft_samples_half*Fs2;
spectre_data = fft_data_sum_log(1:fft_samples_half);
else
spectre_freq_points = (1:fft_samples)/fft_samples*Fs-Fs2;
spectre_data = [fft_data_sum_log(fft_samples_half+1:end) fft_data_sum_log(1:fft_samples_half)];
end
end
plot(spectre_freq_points,spectre_data,plot_color)
axis('tight')
grid on
hold off
title ('Amplitude Frequency Characteristic');
xlabel ('Frequency, MHz');
ylabel ('Amplitude, dB');