0

I am using MATLAB 2021a. I have regression problem. I facing error in using buffer.

Problem is here actually.

Q_signal is 1000x1 double and estimated_Q_signals is 991x1 cell and every cell is 1x10 row vector. so I got this error. Error using plot Invalid data argument. Error in Aws_LSTM_PhaseMeasure (line 69) plot(time(2:end), estimated_Q_signals(2:end), 'DisplayName', 'Estimated Q Signal');

Here is code.

% Parameters
n_samples = 1000;
signal_frequency = 10;
time = linspace(0, 1, n_samples)';
I_signal = cos(2 * pi * signal_frequency * time);
Q_signal = sin(2 * pi * signal_frequency * time);

% Create pairs of I and Q signals
X = I_signal(1:end-1); % I signal as input
y = Q_signal(2:end);   % Corresponding Q signal as target output

% Define sequence length (number of time steps in each sequence)
sequence_length = 10;  % You can adjust this as needed

% Prepare sequences of input and target data
X_sequences = buffer(X, sequence_length, sequence_length-1, 'nodelay').';
y_sequences = buffer(y, sequence_length, sequence_length-1, 'nodelay').';

% Split the dataset into training and testing sets
rng(0); % Set random seed for reproducibility
split_ratio = 0.8;
split_idx = round(split_ratio * size(X_sequences, 1));
X_train = X_sequences(1:split_idx, :);
y_train = y_sequences(1:split_idx, :);
X_test = X_sequences(split_idx+1:end, :);
y_test = y_sequences(split_idx+1:end, :);

% Reshape data for LSTM input (numObservations, numTimeSteps, numFeatures)
X_train = reshape(X_train, [1, size(X_train, 1), sequence_length]);
y_train = reshape(y_train, [1, size(y_train, 1), sequence_length]);
X_test = reshape(X_test, [1, size(X_test, 1), sequence_length]);
y_test = reshape(y_test, [1, size(y_test, 1), sequence_length]);

% Create an LSTM-based neural network
layers = [
    sequenceInputLayer(1) % Sequence input layer for single scalar input
    lstmLayer(64, 'OutputMode', 'sequence')
    lstmLayer(32, 'OutputMode', 'sequence')
    fullyConnectedLayer(1)
    regressionLayer % Regression layer
];

fn=@(z) reshape( num2cell(permute(z,[1,3,2]),2)  ,[],1);
X_train=fn(X_train);
y_train=fn(y_train);
X_test=fn(X_test);
y_test=fn(y_test);

options = trainingOptions('adam', ...
    'MaxEpochs', 50, ...
    'MiniBatchSize', 32, ...
    'ValidationData', {X_test, y_test}, ...
    'Plots', 'training-progress', ...
    'InitialLearnRate', 0.01);

% Train the LSTM network
net = trainNetwork(X_train, y_train, layers, options);

% Use the trained model to estimate Q signals from new I signals
new_I_signals = cos(2 * pi * signal_frequency * time); % Replace with your new I signals
new_X_sequences = buffer(new_I_signals, sequence_length, sequence_length-1, 'nodelay').';
new_X_sequences = reshape(new_X_sequences, [1, size(new_X_sequences, 1), sequence_length]);
estimated_Q_signals = predict(net, fn(new_X_sequences));

% Plot the original Q signals and estimated Q signals
figure;
plot(time(2:end), Q_signal(2:end), '--', 'DisplayName', 'Original Q Signal');
hold on;
plot(time(2:end), estimated_Q_signals(2:end), 'DisplayName', 'Estimated Q Signal');
xlabel('Time');
ylabel('Amplitude');
legend('Location', 'best');
title('Q Signal Estimation with LSTM');
grid on;

1 Answer 1

1

estimated_Q_signals is a cell array in your data. You can open cell arrays as estimated_Q_signals{1} or any other index which extracts what is inside that cell. If you want to dump all the insides into one vector, you can do x = [estimated_Q_signals{:}] but keep in mind that this won't be of the same size as time variable. I guess this is a matter of how much prediction matlab is doing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.