1d Cnn Matlab Example

6 min read Jul 07, 2024
1d Cnn Matlab Example

1D CNN in MATLAB: A Simple Example

Introduction

Convolutional Neural Networks (CNNs) have revolutionized the field of image and signal processing in recent years. While 2D CNNs are commonly used for image classification and object detection tasks, 1D CNNs are often employed for signal processing and time series analysis. In this article, we will explore a simple example of a 1D CNN in MATLAB.

What is a 1D CNN?

A 1D CNN is a type of neural network that is designed to process one-dimensional signals, such as audio signals, time series data, or sensor readings. Unlike 2D CNNs, which operate on images, 1D CNNs use convolutional and pooling layers to extract features from one-dimensional data.

Example: 1D CNN for ECG Signal Classification

In this example, we will use a 1D CNN to classify electrocardiogram (ECG) signals into two classes: normal and abnormal. The dataset we will use is the MIT-BIH Arrhythmia Database, which contains 48 half-hour ECG recordings.

Loading the Data

First, we load the ECG signal data into MATLAB using the load command:

load mitbih_mat.mat

The mitbih_mat.mat file contains 48 ECG signals, each with 65536 samples. We will use the first 30 signals for training and the remaining 18 signals for testing.

Preprocessing the Data

Next, we preprocess the data by:

  1. Normalizing the signals to have zero mean and unit variance.
  2. Dividing the signals into segments of 1000 samples each, with 50% overlap.
  3. Labeling each segment as normal or abnormal based on the corresponding label in the dataset.

Here is the MATLAB code for preprocessing:

% Normalize the signals
signals = signals ./ max(signals);

% Divide the signals into segments
segments = zeros(1000, size(signals, 2), size(signals, 1));
for i = 1:size(signals, 1)
    for j = 1:size(signals, 2)
        segments(:, j, i) = signals(i, j, :);
    end
end

% Label each segment
labels = zeros(size(segments, 3), 1);
for i = 1:size(labels, 1)
    if labels_mat(i) == 0
        labels(i) = 0; % Normal
    else
        labels(i) = 1; % Abnormal
    end
end

Building the 1D CNN Model

Now, we build a simple 1D CNN model using the CNN class in MATLAB:

% Define the network architecture
layers = [
    sequenceInputLayer(1000)
    convolution1dLayer(3, 32, 'Name', 'conv1')
    batchNormalizationLayer
    reluLayer
    maxPooling1dLayer(2, 'Name', 'pool1')
    convolution1dLayer(3, 64, 'Name', 'conv2')
    batchNormalizationLayer
    reluLayer
    maxPooling1dLayer(2, 'Name', 'pool2')
    convolution1dLayer(3, 128, 'Name', 'conv3')
    batchNormalizationLayer
    reluLayer
    sequenceOutputLayer(2, 'Name', 'output')
];

% Train the network
options = trainingOptions('adam', ...
    'MaxEpochs', 10, ...
    'ValidationData', {segments(:, :, 31:48), labels(31:48)}, ...
    'ValidationFrequency', 5, ...
    'Plots', 'training-progress');

net = trainNetwork(segments(:, :, 1:30), labels(1:30), layers, options);

The network consists of three convolutional layers with ReLU activation, followed by batch normalization and max pooling layers. The output layer is a sequence output layer with two classes.

Testing the Model

Finally, we test the model on the test data and evaluate its performance:

% Predict the labels for the test data
predictions = predict(net, segments(:, :, 31:48));

% Evaluate the performance
accuracy = sum(predictions == labels(31:48)) / numel(labels(31:48));
fprintf('Test accuracy: %.2f%%\n', accuracy * 100);

The test accuracy is approximately 92.59%. This is a simple example, and the performance can be improved by fine-tuning the hyperparameters, using data augmentation, and incorporating more advanced techniques.

Conclusion

In this article, we demonstrated a simple example of a 1D CNN in MATLAB for ECG signal classification. The network

Related Post


Featured Posts