-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcase2_nonparametric_freq.m
175 lines (142 loc) · 4.94 KB
/
case2_nonparametric_freq.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
166
167
168
169
170
171
172
173
174
175
% ===========================================================
% NON-PARAMETRIC ESTIMATION: FREQUENCY SIGNAL
% ===========================================================
% This scripts carries out a Welch-based parametric estimation
% using a PMU-recorded voltage signal for forced oscillation
% identification.
%
% Last modification: 02/06/2023 by SADR.
clc
clear
close all
% ---- Loading given data
load('data\sysid_power_case2.mat');
%% Step 0: Visualization
% ---- Setting up plotting parameters
width = 8;
height = 6;
fig = figure('Renderer', 'opengl', 'Position', [10 10 width*100 height*100]);
set(0, 'DefaultLineLineWidth', 1.5);
set(0,'DefaultAxesFontSize', 18)
set(0,'DefaultTextFontSize', 18)
set(0, 'DefaultTextFontName', 'Times')
set(0, 'DefaultAxesFontName', 'Times')
subplot(211)
plot(time, Freq(:, 5), 'color', '#191970')
xlim([0, max(time)])
title('Frequency at Bus 5')
xlabel('Time (s)')
ylabel('$f$ (Hz)', 'interpreter', 'latex')
xline(t_event, 'color', 'red', 'linewidth', 2, 'linestyle', '--')
subplot(212)
plot(time, Freq(:, 13), 'color', '#191970')
xlim([0, max(time)])
title('Frequency at Bus 13')
xlabel('Time (s)')
ylabel('$f$ (Hz)', 'interpreter', 'latex')
xline(t_event, 'color', 'red', 'linewidth', 2, 'linestyle', '--')
exportgraphics(fig, 'results/fig_case2_f-time-plot.pdf', ...
'ContentType', 'vector', 'BackGroundColor', 'none');
%% Step 1: Detrending
% ---- Extracting frequency signals during the oscillation
y1 = Freq(1:end-800, 7);
N1 = size(y1, 1);
t1 = ts:ts:length(y1)*ts;
y2 = Freq(1:end-800, 10);
N2 = size(y2, 1);
t2 = ts:ts:length(y2)*ts;
% ---- Detrending
y1 = detrend(y1);
y2 = detrend(y2);
% ---- Plotting detrended signal during the event
fig = figure('Renderer', 'opengl', 'Position', [10 10 width*100 height*100]);
set(0, 'DefaultLineLineWidth', 1.5);
set(0,'DefaultAxesFontSize', 18)
set(0,'DefaultTextFontSize', 18)
set(0, 'DefaultTextFontName', 'Times')
set(0, 'DefaultAxesFontName', 'Times')
subplot(211)
plot(t1, y1, 'color', '#191970')
xlim([0, max(t2)])
title('Frequency at Bus 7')
xlabel('Time (s)')
ylabel('$f$ (Hz)', 'interpreter', 'latex')
xline(t_event, 'color', 'red', 'linewidth', 2, 'linestyle', '--')
subplot(212)
plot(t2, y2, 'color', '#191970')
xlim([0, max(t2)])
title('Frequency at Bus 10')
xlabel('Time (s)')
ylabel('$f$ (Hz)', 'interpreter', 'latex')
xline(t_event, 'color', 'red', 'linewidth', 2, 'linestyle', '--')
exportgraphics(fig, 'results/fig_case2_f-detrend.pdf', ...
'ContentType', 'vector', 'BackGroundColor', 'none');
%% Step 2: Analyzing Signal Whiteness
% ---- Settings for autocorrelation plots
width = 8;
height = 6;
fig = figure('Renderer', 'opengl', 'Position', [10 10 width*100 height*100]);
set(0, 'DefaultLineLineWidth', 1.5);
set(0,'DefaultAxesFontSize', 18)
set(0,'DefaultTextFontSize', 18)
set(0, 'DefaultTextFontName', 'Times')
set(0, 'DefaultAxesFontName', 'Times')
subplot(211)
autocorr(y1, 'NumLags', round(N1/4));
title('$\sigma_{y_1y_1}\left[\ell\right]$', 'interpreter', 'latex')
subplot(212)
autocorr(y2, 'NumLags', round(N2/4));
title('$\sigma_{y_2y_2}\left[\ell\right]$', 'interpreter', 'latex')
exportgraphics(fig, 'results/fig_case2_f-autocorr.pdf', ...
'ContentType', 'vector', 'BackGroundColor', 'none');
%% Step 3: Welch-based Power Spectral Density (PSD) estimation
% ---- Window-size
Nfft = [1024, 2048, 5012];
linestyle = {'-', '-', '-', '-'};
color = {'#191970', '#6495ED', '#00BFFF', '#ADD8E6'};
% ---- Settings for PSD plots
width = 8;
height = 12;
fig = figure('Renderer', 'opengl', 'Position', [10 10 width*100 height*100]);
set(0, 'DefaultLineLineWidth', 2);
set(0,'DefaultAxesFontSize', 18)
set(0,'DefaultTextFontSize', 18)
set(0, 'DefaultTextFontName', 'Times')
set(0, 'DefaultAxesFontName', 'Times')
for i=length(Nfft):-1:1
subplot(211)
% ---- PSD estimation using Welch method
[Pyy1, f] = pwelch(y1, Nfft(i), [], Nfft(i), fs);
plot(f, 10*log10(Pyy1), linestyle{i}, 'color', color{i}, 'DisplayName', ...
"$N_{FFT}$ = " + string(Nfft(i)))
xlim([0, fs/2])
hold on
if i==1
xlabel('$f$ [Hz]', 'interpreter', 'latex');
ylabel('$10 \log_{10}\Vert \hat{S}_{y_1y_1}^2(f)\Vert$', ...
'Interpreter', 'latex');
title('Welch-based PSD Estimation of $y_1$ ($f_{7}$)', ...
'Interpreter','latex');
lgd = legend('interpreter', 'latex');
lgd.NumColumns = 4;
lgd.Location = 'southoutside';
end
subplot(212)
[Pyy2, f] = pwelch(y2, Nfft(i), [], Nfft(i), fs);
plot(f, 10*log10(Pyy2), linestyle{i}, 'color', color{i}, 'DisplayName', ...
"$N_{FFT}$ = " + string(Nfft(i)))
xlim([0, fs/2])
hold on
if i==1
xlabel('$f$ [Hz]', 'interpreter', 'latex');
ylabel('$10 \log_{10}\Vert \hat{S}_{y_2y_2}^2(f)\Vert$', ...
'Interpreter', 'latex');
title('Welch-based PSD Estimation of $y_2$ ($f_{10}$)', ...
'Interpreter','latex');
lgd = legend('interpreter', 'latex');
lgd.NumColumns = 4;
lgd.Location = 'southoutside';
end
end
exportgraphics(fig, 'results/fig_case2_f-PSD.pdf', ...
'ContentType', 'vector', 'BackGroundColor', 'none');