-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotFFT_plain.m
More file actions
45 lines (37 loc) · 1.6 KB
/
plotFFT_plain.m
File metadata and controls
45 lines (37 loc) · 1.6 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
function plotFFT_plain(matrix, fs, color_)
% Function to plot the Fourier Transform power in dB of a 2D matrix
% Inputs:
% matrix - 2D matrix with rows as channels and columns as time points
% fs - sampling frequency
smooth_factor = 200;
[L, numChannels] = size(matrix);
% Compute the FFT and power for each channel
Y = fft(matrix);
P2 = abs(Y/L);
P1 = P2(1:L/2+1, :);
P1(2:end-1) = 2*P1(2:end-1);
f = fs/L*(0:(L/2));
%
% if contains(title_, "active", 'IgnoreCase', true) && contains(title_, "stimulus", 'IgnoreCase', true)
% range1Indices = find(f >= 59.28 & f <= 59.42);
% range2Indices = find(f >= 59.94 & f <= 60.04);
% for ch = 1:numChannels
% powerInitial = mean(P1(range2Indices, ch));
% powerToTransfer = mean(P1(range1Indices, ch));
% P1(range2Indices, ch) = P1(range2Indices, ch) + powerToTransfer;
% P1(range1Indices, ch) = powerInitial;
% end
% end
% Plot each channel's power in dB in gray
hold on;
% for i = 1:numChannels
% plot(f, smooth(P1(:, i)/mean(P1(:, i), 'all'), smooth_factor), 'Color', [0.8, 0.8, 0.8]); % Gray color
% end
avgPowerDB = mean(mag2db(P1)/2, 2);
% Plot the average power in dB in black
avgPlot = plot(f, smooth(avgPowerDB, smooth_factor), 'Color', color_, 'LineWidth', 2); % Black color
xlim([0, 70])
% Set plot labels, title, and font size
xlabel('Frequency (Hz)', 'interpreter', 'tex', 'FontSize', 16);
ylabel('PSD (10Log_{10}(uV^2))', 'interpreter', 'tex', 'FontSize', 16);
end