LightningChart .NETCreating an Electroencephalograph (EEG) chart with LightningChart .NET

TutorialStep-by-step tutorial how to create your own EEG chart application.

EEG Chart  

The human brain is one of the most complex and fascinating structures in the world. Scientists have been studying the brain for years to better understand how it works and to find ways to diagnose and treat various neurological disorders.

One of the most common tools used in this field is an electroencephalograph (EEG) which is used to measure the electrical activity of the brain. In recent years, with the rise of .NET technologies, developers have been able to create EEG charts that can visualize the electrical activity of the brain in real-time.

In this blog, we will explore what an electroencephalograph is and how .NET developers can develop their own EEG chart applications to process neurological brain activity data.

The human brain is one of the most complex and fascinating structures in the world.

Scientists have been studying the brain for years to better understand how it works and to find ways to diagnose and treat various neurological disorders.

One of the most common tools used in this field is an electroencephalograph (EEG) which is used to measure the electrical activity of the brain.

In recent years, with the rise of .NET technologies, developers have been able to create EEG charts that can visualize the electrical activity of the brain in real-time.

In this blog, we will explore what an electroencephalograph is and how .NET developers can develop their own EEG chart applications to process neurological brain activity data.

Project Overview

The purpose of this project is to develop an application for monitoring electroencephalograph (EEG) data in real time. Specifically, the application will generate an EEG chart that processes scrolling data and estimates power spectral density.

This means that the application will be able to analyze the frequency content of the EEG signals and provide a visual representation of the distribution of spectral density in real time.

The EEG chart will show this information on a brain surface display, allowing the user to see where the most activity is occurring. Overall, the application will enable the user to monitor and analyze EEG data more effectively and efficiently, making it a valuable tool for researchers, medical professionals, and anyone interested in studying the brain.

The purpose of this project is to develop an application for monitoring electroencephalograph (EEG) data in real time.

Specifically, the application will generate an EEG chart that processes scrolling data and estimates power spectral density.

This means that the application will be able to analyze the frequency content of the EEG signals and provide a visual representation of the distribution of spectral density in real time.

The EEG chart will show this information on a brain surface display, allowing the user to see where the most activity is occurring.

Overall, the application will enable the user to monitor and analyze EEG data more effectively and efficiently, making it a valuable tool for researchers, medical professionals, and anyone interested in studying the brain.

zip icon
Download the project to follow the tutorial

Local Setup

For this project, we need to take in count the following requirements to compile the project.

  • OS: 32-bit or 64-bit Windows Vista or later, Windows Server 2008 R2 or later.

  • DirectX: 9.0c (Shader model 3 and higher) or 11.0 compatible graphics adapter.

  • Visual Studio: 2010-2019 for development, not required for deployment.

  • Platform .NET Framework: installed version 4.0 or newer.

Now go to the next URL and click the download button: http://lightningchart.com/net-charts/

Download-LightningChart-.NET-SDK

You will be redirected to a sign in form, from then on, the process is very simple to follow. So, after confirming your email, you will have access to your own LightningChart account.

Example-LightningChart-Account

After you sign into your account, you will be able to download the SDK. This SDK will be a "free trial" version, but you will be able to use many important features.

If you download the SDK, you will have an .exe like this:

LightningChart-.NET-SDK-Setup-Downloader

The installation will be a typical Windows process, so please continue with it until it is finished. After the installation, you will see the following programs:

LightningChart-.NET-Installed-Programs
  • License Manager: In this application, you will see the purchase options. All the projects that you will create with this trial SDK, will be available for future developments with all features enabled.
Purchase-Options-LightningChart-.NET
  • LightningChart .NET Interactive Examples: now you can see 100+ interactive visualizations available for WPF, WinForms, and/or UWP though today we’re working with Smith Charts.
EEG-chart-Interactive-Example

Visual Studio Project

Now let’s work with visual studio. The main difference between using the LightningChart .NET visualizer and Visual Studio, is that we will be able to analyze and experiment with many features in the source code. In the LC visualizer, select the EEG Visualization and run the example:

EEG-chart-Interactive-Example

In the top-right zone of the windows, you will see the following options:

Available options for Visual Studio projects in LightningChart .NET

For trial SDK, we will be able to use the WPF and WinForms frameworks. If you are fully related to windows forms, this option will be more comfortable. In this case I will use the Windows Presentation Foundation framework.

After clicked the framework to use, we will need to specify a folder where the project will be created:

EEG-Chart-Project-Folder

Finally, the project will be created, and the Visual Studio will be opened and ready for execution.

Smiths-charts-project-ready 

Code Review

The main code will be wrapped inside MainWindow.xaml.cs. Here we will find the code for UI controls.

charting application UI controls

Inside the code we will check two methods that will create the properties that we need to draw correctly the chart.

InitializeCharts()

The main method that will orchestrate the functions that will create the EEG, Buffers, Spectrum, and 3D/2D Heads.

  • CreateBuffers:

Creates many buffers with the number of channels specified in the EEGchannelsCount variable. Buffering involves manipulating data that is stored in memory as a sequence of bytes. The bytes are organized into a buffer.

The main method that will orchestrate the functions that will create the EEG, Buffers, Spectrum, and 3D/2D Heads.

  • CreateBuffers:

Creates many buffers with the number of channels specified in the EEGchannelsCount variable.

Buffering involves manipulating data that is stored in memory as a sequence of bytes. The bytes are organized into a buffer.

private void CreateBuffers()
        {
            _buffer = new double[_EEGchannelsCount][];
            _buffer04 = new double[_EEGchannelsCount];
            _buffer48 = new double[_EEGchannelsCount];
            _buffer812 = new double[_EEGchannelsCount];
            _buffer1232 = new double[_EEGchannelsCount];
            _buffer32100 = new double[_EEGchannelsCount];
            for (int i = 0; i < _EEGchannelsCount; i++)
            {
                _buffer[i] = new double[_iWinSizeForFFT];
            }
        }
  • CreateEEG:

Creates the chart object. The type for EEG is the XY chart. It means that we need to build the axes by adding properties to them.

chart.ViewXY.ZoomPanOptions.WheelZooming = WheelZooming.Off;
            chart.ViewXY.ZoomPanOptions.RightToLeftZoomAction = RightToLeftZoomActionXY.Off;
            chart.ViewXY.ZoomPanOptions.DevicePrimaryButtonAction = UserInteractiveDeviceButtonAction.None;
            chart.ActiveView = ActiveView.ViewXY;
            chart.ViewXY.XAxes[0].MinorGrid.Visible = false;
            chart.ViewXY.XAxes[0].MajorGrid.Visible = false;
            chart.ViewXY.XAxes[0].AllowUserInteraction = false;
            chart.ViewXY.XAxes[0].AutoDivSpacing = false;
            chart.ViewXY.XAxes[0].MajorDiv = 10;
            chart.ViewXY.XAxes[0].Title.Text = "Time (s)";
            chart.ViewXY.AxisLayout.AutoAdjustMargins = false;
            chart.ViewXY.Margins = new Thickness(65, 7, 10, 50);
            chart.ViewXY.YAxes.Clear();

We can add properties to each data from the output3min.csv file.

EEG-Chart-Properties
for (int i = 0; i < _reader.data.Count; i++)
            {
                chart.ViewXY.YAxes.Add(new AxisY());
                chart.ViewXY.YAxes[chart.ViewXY.YAxes.Count - 1].Minimum = -2000;
                chart.ViewXY.YAxes[chart.ViewXY.YAxes.Count - 1].Maximum = 2000;
                chart.ViewXY.YAxes[chart.ViewXY.YAxes.Count - 1].Title.Text = _reader.data[i].Item1;
                chart.ViewXY.YAxes[chart.ViewXY.YAxes.Count - 1].MinorGrid.Visible = false;
                chart.ViewXY.YAxes[chart.ViewXY.YAxes.Count - 1].MajorGrid.Visible = false;
                chart.ViewXY.YAxes[chart.ViewXY.YAxes.Count - 1].AllowUserInteraction = false;
                chart.ViewXY.YAxes[chart.ViewXY.YAxes.Count - 1].ScaleNibs.Size = new SizeDoubleXY(0, 0);
                chart.ViewXY.YAxes[chart.ViewXY.YAxes.Count - 1].Title.AllowUserInteraction = false;
                chart.ViewXY.SampleDataBlockSeries.Add(new SampleDataBlockSeries());
                chart.ViewXY.SampleDataBlockSeries[i].AllowUserInteraction = false;
                chart.ViewXY.SampleDataBlockSeries[i].AssignYAxisIndex = i;
                chart.ViewXY.YAxes[chart.ViewXY.YAxes.Count - 1].LabelsColor = chart.ViewXY.YAxes[chart.ViewXY.YAxes.Count - 1].AxisColor
                    = chart.ViewXY.YAxes[chart.ViewXY.YAxes.Count - 1].Title.Color
                    = chart.ViewXY.SampleDataBlockSeries[i].Color = ChartTools.ColorHSVA(i * 11);
                chart.ViewXY.SampleDataBlockSeries[i].SamplingFrequency = _samplingFrequency;
                chart.ViewXY.SampleDataBlockSeries[i].FirstSampleTimeStamp = 1 / _samplingFrequency;
            }
  • Adding the EEG object to the grid

ChartPlaceEEG.Children.Add(chart);
            _ChartList.Add(chart);
<Grid Grid.Column="0" x:Name="ChartPlaceEEG"></Grid>
  • CreatePowerSpectrum:

This method will create the power spectrum lines, based on the same logic we used for EEG. In this method, we will use the Annotation class. This constructor will let us bind visual properties to a float text box in each data axis.

  • CreatePowerSpectrum:

This method will create the power spectrum lines, based on the same logic we used for EEG.

In this method, we will use the Annotation class.

This constructor will let us bind visual properties to a float text box in each data axis.

EEG-Chart-Annotation-Class-Method
AnnotationXY anno = new AnnotationXY();
            anno.AllowDragging = false;

            anno.Style = AnnotationStyle.RoundedRectangle;
            anno.AllowResize = false;
            anno.BorderVisible = false;
            anno.Fill.Color = Colors.OrangeRed;
            anno.TextStyle.Color = Colors.Black;
            anno.AllowRotate = false;
            anno.AllowTargetMove = false;
            anno.AllowAnchorAdjust = false;
            anno.Visible = false;
            anno.AllowUserInteraction = false;
            anno.ClipInsideGraph = false;
            chart.ViewXY.Annotations.Add(anno);
            chart.ViewXY.XAxes[0].Minimum = 0;
            chart.ViewXY.XAxes[0].Maximum = _iCutOffFR;
            chart.ViewXY.AxisLayout.YAxesLayout = YAxesLayout.Stacked;
            chart.MouseMove += Chart_MouseMove;
            ChartPlacePowerSpectrum.Children.Add(chart);
            _ChartList.Add(chart);
  • Adding the power spectrum to the grid

ChartPlacePowerSpectrum.Children.Add(chart);
            _ChartList.Add(chart);
<Grid Grid.Column="1" x:Name="ChartPlacePowerSpectrum"></Grid>
  • Create3DHead:

This method will create the 3D head and 3D brain.

EEG-Chart-Create3DHead
We must use some 3D constructors from LightningChart .NET to create this object.

  • MeshModel. The mesh model constructor will create the vertices, edges, and faces used in the polygonal representation to define a 3D shape.

The mesh model will need a 3D object to start the process:

MeshModel MeashBrains = new MeshModel();
            MeashBrains.LoadFromFile(_Brains);
            MeashBrains.AllowUserInteraction = false;
            MeashBrains.Position = new PointDoubleXYZ(50, 45 /*-360*/, 41);
            MeashBrains.Size.Width = 70;
            MeashBrains.Size.Depth = 70;
            MeashBrains.Size.Height = 70;

The [_Brains] string variable, contains the path to the 3D object used for this template. The path by default could be: C:\ProgramData\Arction\Content

EEG-Chart-Default-Path

If you open the 3D object, by default this will be shown in the windows 3D viewer (windows 10-11):

EEG-Chart-Default-3D-Object-View

Once we loaded the 3D object, we can add UI properties by using the View3D, SurfaceMeshSeries3D, and Annotation3D constructors.

  • Adding data points to the 3D object:

_dataPointSeries = new PointLineSeries3D(chart.View3D, Axis3DBinding.Primary, Axis3DBinding.Primary, Axis3DBinding.Primary)
            {
                Points = _points
            };

The [PointLinSeries3d] constructor, will help us to add values and their UI properties. The constructor will need an array of points:

private SeriesPoint3D[] MakeDataPoints()
        {
            SeriesPoint3D[] points = new SeriesPoint3D[] { 
                 // AF3
                 new SeriesPoint3D(59.95, 68.40, 18.33),
                 // AF4
                 new SeriesPoint3D(40.19, 68.40, 18.33),
                 // F5

For this exercise, the data points are hardcoded in the [MakeDataPoints] array function. To display the 3D chart in the grid, the Annotation3D object will be added to the View3D constructor.

For this exercise, the data points are hardcoded in the [MakeDataPoints] array function. 

To display the 3D chart in the grid, the Annotation3D object will be added to the View3D constructor.

chart.View3D.Annotations.Add(anno);
            ChartPlace3D.Children.Add(chart);
            _ChartList.Add(chart);

The ChartPlace3D object, refers to the grid:

<Grid x:Name="ChartPlace3D"></Grid>
<Grid Grid.Column="1">
  • Create2DHead:

EEG-Chart-2DHead

The way to create these heads is basically to take a .png (content.head.png) image and draw the brain contour, based on many X-Y points (headContour.csv).

  • EEG Points locations.

To add sensor locations is necessary to create an array of [SeriesPoint] struct. The [SeriesPoint] structure needs an X – Y position and a Tag.

SeriesPoint[] MakeEEG2D_Locations()
        {
            List<SeriesPoint> listPoints = new List<SeriesPoint>();
            //AF3
            SeriesPoint AF3 = new SeriesPoint(-23.6326, 60.551);
            AF3.Tag = "AF3";
            listPoints.Add(AF3);

            //AF4 
            SeriesPoint AF4 = new SeriesPoint(19.2945, 60.511);
            AF4.Tag = "AF4";
            listPoints.Add(AF4);
  • Creating the EEG points:

To create points, we can use the [FreeFormPointLineSeries] constructor. You can add style properties to draw the points as you need.

seriesSensorPositions = new FreeformPointLineSeries(v, _xAxis, _yAxis);
            seriesSensorPositions.LineVisible = false;
            seriesSensorPositions.AllowUserInteraction = true;
            seriesSensorPositions.Visible = true;
            seriesSensorPositions.Highlight = Highlight.None;
            seriesSensorPositions.PointStyle.Shape = Arction.Wpf.Charting.Shape.Circle;
            seriesSensorPositions.PointStyle.GradientFill = GradientFillPoint.Solid;
            seriesSensorPositions.PointStyle.Width = 11;
            seriesSensorPositions.PointStyle.Height = 11;
            seriesSensorPositions.ShowInLegendBox = false;

Once we have the properties and locations of the points, we can create the markers using the [SeriesEventMarker] constructor.

int activeSensorsCount = _sensorLocations.Length;

            for (int i = 0; i < activeSensorsCount; i++)
            {
                SeriesPoint p = _sensorLocations[i];

                SeriesEventMarker marker = new SeriesEventMarker(seriesSensorPositions);
                marker.Symbol.Color1 = Colors.Gray;
                marker.Symbol.GradientFill = GradientFillPoint.Solid;
                marker.VerticalPosition = SeriesEventMarkerVerticalPosition.AtYValue;
                marker.HorizontalPosition = SeriesEventMarkerHorizontalPosition.AtXValue;
                marker.AllowUserInteraction = false;
                marker.AllowDragging = true;
                marker.Symbol.Shape = SeriesMarkerPointShape.Circle;
                marker.Symbol.Width = 4;
                marker.Symbol.Height = 4;
                marker.Symbol.BorderWidth = 0;
                marker.XValue = _sensorLocations[i].X;
                marker.YValue = _sensorLocations[i].Y;
                marker.Label.Text = p.Tag.ToString();
                marker.Label.HorizontalAlign = AlignmentHorizontal.Center;
                marker.Label.VerticalAlign = AlignmentVertical.Top;
                seriesSensorPositions.SeriesEventMarkers.Add(marker);
            }

Final Application

In conclusion, LightningChart .NET EEG chart is an exceptional tool for visualizing and analyzing electroencephalography (EEG) data in real time. It provides a variety of features such as high-speed rendering, customizable visualizations, and extensive data analysis capabilities, making it an ideal solution for a wide range of EEG applications.

With LightningChart .NET EEG chart, researchers, clinicians, and other EEG professionals can gain valuable insights into the brain’s electrical activity with ease. They can quickly and accurately identify patterns, trends, and anomalies in the data, making it easier to diagnose and treat a variety of neurological conditions.

Moreover, the user-friendly interface and simple integration make it easy for users to get started with the tool, regardless of their level of expertise. Overall, LightningChart .NET EEG chart is a powerful and effective tool for EEG data visualization and analysis, making it an excellent choice for anyone looking to gain a deeper understanding of brain activity.

See you in the next article!

In conclusion, LightningChart .NET EEG chart is an exceptional tool for visualizing and analyzing electroencephalography (EEG) data in real time.

It provides a variety of features such as high-speed rendering, customizable visualizations, and extensive data analysis capabilities, making it an ideal solution for a wide range of EEG applications.

With LightningChart .NET EEG chart, researchers, clinicians, and other EEG professionals can gain valuable insights into the brain’s electrical activity with ease.

They can quickly and accurately identify patterns, trends, and anomalies in the data, making it easier to diagnose and treat a variety of neurological conditions.

Moreover, the user-friendly interface and simple integration make it easy for users to get started with the tool, regardless of their level of expertise. 

Overall, LightningChart .NET EEG chart is a powerful and effective tool for EEG data visualization and analysis, making it an excellent choice for anyone looking to gain a deeper understanding of brain activity.

See you in the next article!

Omar Urbano Software Engineer

Omar Urbano

Software Engineer

LinkedIn icon
divider-light

Continue learning with LightningChart