-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathplotFFT.m
More file actions
66 lines (62 loc) · 2.01 KB
/
plotFFT.m
File metadata and controls
66 lines (62 loc) · 2.01 KB
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
function plotFFT(time, x, argt, ylbl, rng, path)
% Inputs:
% time - time stamp of data x
% x - Sensor data
% argt - Title of plot
% ylbl - Ylabel of plot
% rng - range of data want to look at
% path - path to save files
% Example:
% plotUlogs("log001.ulg","Test1")
% plotUlogs("001_Test1\")
%
% Author: Kyuhyong You
% Website: https://github.com/kyuhyong/plotulog
% Jan 2019/1/8;
% Revision: -
%------------- BEGIN CODE --------------
% Strip data when none zero range is entered
if(rng != 0)
startIdx = 0;
tailIdx = 0;
idx = 1;
while(tailIdx==0)
if(idx>length(time)) disp("Err: Out of range"); return; endif
if(time(idx) < rng(1)) startIdx++; endif
if(time(idx) > rng(2)) tailIdx = idx; endif
idx++;
endwhile
time = time(startIdx:tailIdx);
x = x(startIdx:tailIdx);
endif
saveName = sprintf("%sFFT_of_%s", path, argt)
h_fft=figure;
clf(h_fft);
subplot(2,1,1);
plot(time, x,"LineWidth", 1.0);
grid on;
set (gca, "xminorgrid", "on"); set (gca, "yminorgrid", "on");
xlabel("Time (sec)");
ylabel(ylbl);
title(argt);
Ts = mean(diff(time)); % Sampling period
Fs = 1/Ts; % Sampling Frequency
Fn = Fs/2; % Niquist Frequency
N = length(x); % Length of signal
% Apply fourrie transfer
Y = fft(x);
FT_Signal = fft(x) / N; % Normalized Fourier Transform
Fv = linspace(0, 1, fix(N/2)+1)*Fn; % Frequency vector
Iv = 1:length(Fv); % Index vector
strmatrix = [sprintf("Fs = %.2f Hz", Fs); sprintf("Fn = %.2f Hz",Fn); sprintf("N = %d",N)];
subplot(2,1,2);
plot(Fv, abs(FT_Signal(Iv))*2, 'r', 'LineWidth', 1.0);
ylim( [ 0.0 0.5 ]);
%plot(f, Pxx, 'r', 'LineWidth', 1.5);
grid on;
xlabel("Frequency (Hz)"); ylabel("|P1(f)|");
title(sprintf("Single-Sided Amplitude Spectrum of %s", argt));
annotation("textbox",[.7 .3 .4 .2], "string", strmatrix);
print(h_fft, saveName, "-dpdf","-color","-S400,800");
print(h_fft, saveName, "-dpng","-color", "-r200");
endfunction