Commit 7b4ca3a9 authored by hazrmard's avatar hazrmard
Browse files

refactored matlab files in separate directory

parent 1d964d61
classdef CompositeModel
%COMPOSITEMODEL Combines black- and grey- box models.
% Detailed explanation goes here
properties
blackBox
greyBox
modelType
end
methods
function obj = CompositeModel(blackBox, greyBox, modelType)
%UNTITLED Construct an instance of this class
% Detailed explanation goes here
obj.blackBox = blackBox;
obj.greyBox = greyBox;
obj.modelType = modelType;
end
function outputArg = method1(obj,inputArg)
%METHOD1 Summary of this method goes here
% Detailed explanation goes here
outputArg = obj.Property1 + inputArg;
end
end
end
......@@ -20,7 +20,7 @@ in = t' * peak / duration;
% sim_in = [t', awgn(in, noise, 'measured')];
sim_in = [t', in];
data = runSim('RC', sim_in);
save("resRC_ramp_" + num2str(noise) + ".mat",...
save("RC_ramp_" + num2str(noise) + ".mat",...
'data', 'peak', 'resolution');
% sine input
......@@ -28,14 +28,14 @@ in = sin(2*pi*freq*t') * peak;
% sim_in = [t', awgn(in, noise, 'measured')];
sim_in = [t', in];
data = runSim('RC', sim_in);
save("resRC_sin_" + num2str(noise) + ".mat",...
save("RC_sin_" + num2str(noise) + ".mat",...
'data', 'freq', 'peak', 'resolution');
% combined ramp + sine + noise
in = (t' * peak / duration) + sin(2*pi*freq*t') * peak;
sim_in = [t', awgn(in, noise, 'measured')];
data = runSim('RC', sim_in);
save("resRC_comb_" + num2str(noise) +".mat",...
save("RC_comb_" + num2str(noise) +".mat",...
'data', 'freq', 'peak', 'resolution');
clear data freq peak N t duration resolution in sim_in
\ No newline at end of file
......@@ -8,11 +8,12 @@
close all;
% Load training data
rcdat = load('resRC_comb_250.mat');
rcdat = load('RC_comb_250.mat');
t = rcdat.data(:,1); % timestamps
u = rcdat.data(:,2); % input
y = rcdat.data(:,3) * 1000; % output
% Normalizing input signal
mu = mean(u);
sig = std(u);
stdU = (u - mu) / sig;
......@@ -53,6 +54,7 @@ options = trainingOptions('adam', ...
net = trainNetwork(XTrain, YTrain, layers, options);
% Plot predicted and actual output
YPred = cell2mat(predict(net, {u'}));
plot(t, y, t, YPred);
legend('Actual', 'Prediction');
\ No newline at end of file
% Training an LSTM network to predict a sequence
% of numbers following an input sequence.
% Training an LSTM network to predict the derivative
% of a sequence.
%
% Input: 1,2,3
% Output: 2,3,4
% Input: 1,2,5,3
% Output: 1,1,3,-2
N = 1000;
NTest = 200;
seqLen = 10;
seq = reshape([1:N], seqLen, [])';
testSeq = reshape([N+1:N+NTest], seqLen, [])';
NTest = N;
seqLen = N;
range = 100;
rng(0);
numbers = randi([0, range], 1, N+NTest);
gradients = gradient(numbers);
seq = reshape(numbers(1:N), seqLen, [])';
seqY = reshape(gradients(1:N), seqLen, [])';
testSeq = reshape(numbers(N+1:end), seqLen, [])';
testSeqY = reshape(gradients(N+1:end), seqLen, [])';
% The input variables are normalized to have 0 mean
% and 1 variance.
mu = mean(seq(:));
......@@ -18,14 +24,13 @@ stdSeq = (seq - mu) / sig;
stdTest = (testSeq - mu) / sig;
% Preparing input data as a cell array of row vectors.
XTrain = mat2cell(stdSeq,ones(1, N/seqLen));
XTrain = mat2cell(stdSeq, ones(1, N/seqLen));
XTest = mat2cell(stdTest, ones(1, NTest/seqLen));
% For format of the response, see
% https://www.mathworks.com/help/deeplearning/ref/trainnetwork.html
% The labels are a cell array of sequences offset by 1
YTrain = mat2cell(seq + 1, ones(1, N/seqLen));
YTest = mat2cell(testSeq + 1, ones(1, NTest/seqLen));
YTrain = mat2cell(seqY, ones(1, N/seqLen));
YTest = mat2cell(testSeqY, ones(1, NTest/seqLen));
% Defining an LSTM network. An LSTM layer takes as
% input `numHiddenUnits` which is the number of time
......@@ -33,21 +38,24 @@ YTest = mat2cell(testSeq + 1, ones(1, NTest/seqLen));
% over which the LSTM layer operates.
layers = [...
sequenceInputLayer(1)
lstmLayer(10,'OutputMode','sequence')
lstmLayer(1,'OutputMode','sequence')
lstmLayer(1,'OutputMode','sequence')
fullyConnectedLayer(1)
regressionLayer];
options = trainingOptions('adam', ...
'MaxEpochs',150, ...
'MaxEpochs',100, ...
'MiniBatchSize', 5, ...
'GradientThreshold',1, ...
'InitialLearnRate',1.0, ...
'InitialLearnRate',1, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',20, ...
'LearnRateDropFactor',0.5, ...
'LearnRateDropPeriod',50, ...
'LearnRateDropFactor',0.75, ...
'Verbose',0, ...
'Plots','training-progress');
net = trainNetwork(XTrain, YTrain, layers, options);
YPred = int32(cell2mat(predict(net, XTrain)));
\ No newline at end of file
YPred = int32(cell2mat(predict(net, XTest)));
plot(1:NTest, YPred, 1:NTest, gradients(N+1:end));
legend('Predicted', 'Actual');
\ No newline at end of file
% Estimates parameters of an RC circuit. Produces an
% Estimates parameters of an RC circuit. Produces an
% `idgrey` instance called `sys` representing the
% learned system parameters.
%
......@@ -13,7 +13,7 @@ par = {'resistance',500; 'capacitance', 1e-3};
sys_init = idgrey(@ssRC, par, 'c');
sys_init.Structure.Parameters(2).Free = false;
rcdat = load('resRC_comb.mat');
rcdat = load('RC_comb_25.mat');
t = rcdat.data(:,1); % timestamps
u = rcdat.data(:,2); % input
y = rcdat.data(:,3); % output
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment