LSTM controller for three phase inverter
20 views (last 30 days)
Show older comments
Dear sir,
I want to use the LSTM network as a controller of a three-phase inverter.
The lstm controller will have six inputs, 3 sinuocidal voltage signals and 3 sinuocidal current signals. (400Hz)
The lstm controller will have three outputs, 20kHZ PWM switching signals. [0-1 at 20kHZ]
I attached lstm1.m, input.mat, output.mat and time.mat files.
I trained LSTM with this files.
My problem is outputs of LSTM controller is not 20kHZ PWM. it is 400Hz sinuocidal signal.
LSTM output must be like "True PWM" figure (20KHz PWM). But the LSTM generates 400Hz sin signals (Predicted PWM).
How can I generate 20kHZ PWM signal at output of LSTM controller
clear;
clc;
load("input.mat");
load("output.mat");
load("Time.mat")
input=rescale(input); % Girişleri 0-1 aralığına ölçekle
output=rescale(output); % Çıkışları 0-1 aralığına ölçekle
inputSize = 6; % 3 voltage + 3 current
numHiddenUnits = 100;
numResponses = 3; % 3 PWM çıkışı
layers = [ ...
sequenceInputLayer(inputSize)
lstmLayer(numHiddenUnits,'OutputMode','sequence')
fullyConnectedLayer(numResponses)
sigmoidLayer % PWM çıkışı [0-1] arası
regressionLayer];
options = trainingOptions('adam', ...
'MaxEpochs',30, ...
'MiniBatchSize', 128, ...
'InitialLearnRate',1e-3, ...
'SequenceLength','longest', ...
'Plots','training-progress', ...
'Verbose',0);
%training
XTrain = {input'}; % [features x timesteps]
YTrain = {output'}; % [outputs x timesteps]
net = trainNetwork(XTrain, YTrain, layers, options);
% Testing
YPred = predict(net, XTrain, 'MiniBatchSize', 1);
YPred = YPred{1}; % [3 x numSamples]
figure;
plot(Time,output); title('True PWM');
figure;
plot(Time,YPred); title('Predicted PWM');
0 Comments
Answers (2)
Shishir Reddy
on 17 Jul 2025
Hi @Bahadir
As per my understanding, you are attempting to train an LSTM neural network in MATLAB to act as controller for a three-phase invertor.
The issue stems from a fundamental difference between the nature of your desired output (high-frequency PWM switching signals) and how LSTM networks typically behave.
The current setup uses an LSTM with a regression output, which causes the network to produce smooth, low-frequency sinusoidal signals, not the sharp binary PWM switching you expect. This behavior is typical because LSTMs trained with regression loss (like MSE) tend to approximate sharp transitions with smooth curves.
To generate true PWM signals (binary 0 or 1 at 20KHz), the problem can be framed as classification task. Kindly refer to the following steps to understand how it can be done.
1. Convert PWM Outputs to Class Labels (0 or 1)
Assuming the target 'output' is originally in [0, 1], convert it to binary class labels:
output = round(output); % Convert continuous values to 0 or 1
outputCategorical = categorical(output); % Use categorical labels
YTrain = {outputCategorical'}; % Transpose to match [outputs x time]
2. Update the Network Architecture
Replace the 'sigmoidLayer' and 'regressionLayer' with classification layers. Here is the modified layer setup:
layers = [ ...
sequenceInputLayer(inputSize)
lstmLayer(numHiddenUnits,'OutputMode','sequence')
fullyConnectedLayer(numResponses)
softmaxLayer
classificationLayer];
3. Train with Classification Loss
Continue using 'trainingOptions' as before. The 'classificationLayer' will automatically use cross-entropy loss during training.
This approach forces the LSTM to output class probabilities at each time step (i.e., "0" or "1"), which is better suited for learning and reproducing binary PWM signals than regression.
I hope this helps.
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!