Skip to content
This repository was archived by the owner on Jun 7, 2025. It is now read-only.

Commit 6d18ca6

Browse files
committed
New function to plot. Renamed normspectrogram
1 parent b1713ea commit 6d18ca6

File tree

2 files changed

+86
-44
lines changed

2 files changed

+86
-44
lines changed

normspectrogram.m renamed to norm_spectrogram.m

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1-
function matrix = normspectrogram(t, acc, regname)
2-
% NORMSPECTROGRAM(T, ACC)
3-
% t: Time array
4-
% acc: Acceleration array (g)
5-
% regname: Name of the seismic file
6-
%
7-
% This function create a normalized spectrogram matrix by doing the following
8-
% algorithm:
1+
function [matrix, varargout] = norm_spectrogram(t, acc)
2+
% NORMSPECTROGRAM This function create a normalized spectrogram matrix by
3+
% doing the following algorithm:
94
%
105
% Each spectrum is divided on 5-second windows overlapped by 2.5 seconds
116
% (50%), on each window is applied:
12-
% 1. Baseline correction
13-
% 2. Tuckey window is applied with r=5%.
14-
% 3. FFT on window signal.
15-
% 4. Spectrum is smoothed by 5 points halfwidth moving average.
16-
% 5. Each element of spectrum is normalized by maximum epsectral amplitude.
7+
% 1. Baseline correction
8+
% 2. Tuckey window is applied with r=5%.
9+
% 3. FFT on window signal.
10+
% 4. Spectrum is smoothed by 5 points halfwidth moving average.
11+
% 5. Each element of spectrum is normalized by maximum epsectral
12+
% amplitude.
13+
%
14+
% Usage:
15+
% matrix = normspectrogram(t, acc)
16+
% [matrix, matrix_t, matrix_f] = normspectrogram(t, acc)
17+
%
18+
% Where:
19+
% t: Time array
20+
% acc: Acceleration array (g)
1721
%
1822
% Author: Pablo Pizarro @ppizarror.com, 2017.
1923
%
@@ -31,7 +35,7 @@
3135
% along with this program; if not, write to the Free Software
3236
% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
3337

34-
%% Constantes
38+
%% Constants
3539
MAX_FREQ = 14; % Max frequency of the matrix
3640
NORMALIZE = true; % Do normalize windows
3741
WINDOW_TIME = 5; % Time of each window
@@ -74,7 +78,6 @@
7478
end
7579

7680
%% Create matrix
77-
7881
% Calculate matrix length by time
7982
t_ini = 0;
8083
tarrsize = 1;
@@ -98,6 +101,8 @@
98101
t_ini = 0; % Initial time
99102
t_indx = 1; % Index of initial time on t array
100103
u = 1; % Number of window
104+
105+
% Start iteration
101106
while true
102107

103108
% Create window and acceleration window
@@ -147,7 +152,7 @@
147152
matrix(freq, u) = acc_norm(freq);
148153
end
149154

150-
% Advance initial window time
155+
%% Advance initial window time
151156
t_ini = t_ini + WINDOW_TIME_MOVEMENT;
152157
if t_ini + WINDOW_TIME > t(end)
153158
break;
@@ -157,34 +162,9 @@
157162

158163
end
159164

160-
%% If regname is not defined
161-
if ~exist('regname', 'var')
162-
regname = '';
163-
end
164-
165-
%% Plot matrix
166-
figure();
167-
168-
% Plot matrix
169-
subplot(2, 1, 1);
170-
title(regname);
171-
hold on;
172-
pcolor(matrix_t, matrix_f, matrix); shading interp; % Plot colormap
173-
[~, h] = contour(matrix_t, matrix_f, matrix, [0.8, 0.8]); % Plot contour at 80%
174-
h.LineWidth = 0.5;
175-
h.LineColor = 'red';
176-
hold off;
177-
ylabel('frequency (Hz)');
178-
xlim([0 matrix_t(end)]);
179-
ylim([0.4 matrix_f(end)]);
180-
181-
% Plot seismic
182-
subplot(2, 1, 2);
183-
plot(t, acc, 'k');
184-
xlabel('Time (s)');
185-
ylabel('a (g)');
186-
xlim([0 t(end)]);
187-
grid on;
165+
%% Store results in varargout
166+
varargout{1} = matrix_t;
167+
varargout{2} = matrix_f;
188168

189169
end
190170

plot_norm_matrix.m

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
function plot_norm_matrix(m, mt, mf, t, acc, regname)
2+
% PLOT_NORM_MATRIX Plot normalized spectrogram alonside acceleration
3+
% data.
4+
%
5+
% Usage:
6+
% plot_norm_matrix(m, mt, mf, t, acc, regname)
7+
%
8+
% Where:
9+
% m: Normalized matrix
10+
% mt: Time array from matrix
11+
% mf: Frequency array from matrix
12+
% t: Time of the seismic data
13+
% acc: Acceleration of seismic data
14+
% regname: Name of the seismic data (plot title)
15+
%
16+
% Author: Pablo Pizarro @ppizarror.com, 2017.
17+
%
18+
% This program is free software; you can redistribute it and/or
19+
% modify it under the terms of the GNU General Public License
20+
% as published by the Free Software Foundation; either version 2
21+
% of the License, or (at your option) any later version.
22+
%
23+
% This program is distributed in the hope that it will be useful,
24+
% but WITHOUT ANY WARRANTY; without even the implied warranty of
25+
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26+
% GNU General Public License for more details.
27+
%
28+
% You should have received a copy of the GNU General Public License
29+
% along with this program; if not, write to the Free Software
30+
% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31+
32+
%% If regname is not defined
33+
if ~exist('regname', 'var')
34+
regname = '';
35+
end
36+
37+
%% Plot matrix
38+
figure();
39+
40+
%% Plot matrix
41+
subplot(2, 1, 1);
42+
title(regname);
43+
hold on;
44+
pcolor(mt, mf, m); shading interp; % Plot colormap
45+
[~, h] = contour(mt, mf, m, [0.8, 0.8]); % Plot contour at 80%
46+
h.LineWidth = 0.5;
47+
h.LineColor = 'red';
48+
hold off;
49+
ylabel('frequency (Hz)');
50+
xlim([0 mt(end)]);
51+
ylim([0.4 mf(end)]);
52+
53+
%% Plot seismic
54+
subplot(2, 1, 2);
55+
plot(t, acc, 'k');
56+
xlabel('Time (s)');
57+
ylabel('a (g)');
58+
xlim([0 t(end)]);
59+
grid on;
60+
61+
end
62+

0 commit comments

Comments
 (0)