CPU High Usage with time for serial port - c#

Iam currently working on a project where i have to read serial port continuously. The data is coming continuously for a max of 45 min. I have to validate the data via checksum and create a packet of 79 bytes. After this i have to plot the data(Trajectory) on a real time basis. The problem with the code is that at start it uses 20% of the CPU usage(Pentium 4, 3.0 GHz, Hyper threading)(which i think is still high) but with time the CPU usage increases and at end it reaches to 60%.
The data is coming in with a baud rate of 115200 and is sent continuously at the rate 100 msec.
My code for reading, validating and plotting is as follows:
The following function receive the data and validate it...
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
header1 = serialPort1.ReadByte();
if (header1 == 0)
header2 = serialPort1.ReadByte();
if ((header1 == 0) && (header2 == 1))//Store the data in an array.
{
for (int i = 0; i < 77; i++)
abudata[i] = serialPort1.ReadByte();
tail = abudata[76];
}
else
{
serialPort1.DiscardInBuffer();
}
checksum = 1;// Calculate the checksum.
for (i = 0; i < 74; i++)
checksum = checksum + (abudata[i]);
checksum1 = (abudata[75] << 8);
checksum1 = checksum1 + (abudata[74]);
if ((checksum == checksum1) && (tail == 4))
this.Invoke(new EventHandler(Display_Results));// Function to display
}
catch (Exception ode)
{
l4[4].BackColor = Color.Red;
}
}
The following function display the data on labels and draw the trajectory on a picture box
private void Display_Results(object s, EventArgs e)
{
head1[0] = header1;
head1[1] = header2;
for (k = 0; k < 77; ++k)
head1[k + 2] = (((int)abudata[k]) & 0x000000ff);
jk = 0;
for (k = 0; k < 36; ++k) //Data packing into 36 bytes
{
num_1[k] = (ulong)((head1[jk + 1]) + (head1[jk] << 8)) & 0x0000ffff;
num_1[k] = (double)num_1[k];
num_2[k] = (double)num_1[k];
jk = jk + 2;
signbit = (int)num_1[k] >> 15;
if (signbit == 1)
{
sgnval = -1;
num_1[k] = num_1[k] - 65535;
num_1[k] = num_1[k] * (-1.0);
}
else
sgnval = 1;
//Converting the data into engineering values
engval[k] = Math.Round(num_1[k] * parammaxval[k] * sgnval / 32767.0, 3);
if (k == 14)
{
try
{
curr_x = (pictureBox2.Width / 2) + (int)((engval[13] * (pictureBox2.Width)) / map_width);
curr_y = (pictureBox2.Height / 2) - (int)((engval[14] * (pictureBox2.Height)) / map_height);
PointF p1 = new Point(curr_x, curr_y);
if (_gPath != null && _gPath.PointCount > 0)
p1 = _gPath.PathPoints[_gPath.PathPoints.Length - 1];
PointF p2 = new Point(curr_x, curr_y);
_gPath.AddLine(p1, p2);
pictureBox2.Invalidate();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}

I extensively work with serial port connected devices and may assure you, that regular using of SerialPort class by itself doesn't produce high CPU load.
FIRST I would suggest you to PROFILE your application. There are a bunch of profilers for .NET
Only after profiling I would suggest to decouple SerialPort reading and data processing. Use Producer Consumer pattern. Put data from SerialPort to queue and consume it from other thread.
That what I have in SerialPortDataReceived function in one of my projects
private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
{
lock (SyncObject)
{
if (!_serialPort.IsOpen) return;
try
{
int toread = _serialPort.BytesToRead;
byte[] bytes = new byte[toread];
_serialPort.Read(bytes, 0, toread);
ProducerAddBytes(bytes);
}
catch (TimeOutException)
{
//logic
}
}
}
P.S. But PROFILE FIRST!

i got what was the problem with the above code..
I am using Graph path for plotting the trajectory
if (k == 14)
{
try
{
curr_x = (pictureBox2.Width / 2) + (int)((engval[13] * (pictureBox2.Width)) / map_width);
curr_y = (pictureBox2.Height / 2) - (int)((engval[14] * (pictureBox2.Height)) / map_height);
PointF p1 = new Point(curr_x, curr_y);
if (_gPath != null && _gPath.PointCount > 0)
p1 = _gPath.PathPoints[_gPath.PathPoints.Length - 1];
PointF p2 = new Point(curr_x, curr_y);
_gPath.AddLine(p1, p2);
pictureBox2.Invalidate();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Now as the application continues to run, it gathers lots of graph points and hence plotting this huge number of point is consuming the resources.
Can anybody suggest me solution to this problem that how to plot the trajectory without slowing the system...

Related

Using Pass by value or Pass by Reference?

I have two methods in my code. Below is one of them.
private async void Characteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args)
{
var newValue = FormatValueByPresentation(args.CharacteristicValue, presentationFormat);
var message = $"Value at {DateTime.Now:hh:mm:ss.FFF}: {newValue}";
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() => CharacteristicLatestValue.Text = message);
}
And it's printing time (Value At) like this.
Now, this is the second method.
private static ushort ParseHeartRateValue(byte[] data)
{
const byte heartRateValueFormat = 0x04;
byte flags = data[0];
ushort offset = 1;
bool HRC2 = (flags & 0x80) > 0;
if (HRC2) //if BPM is un uint16
{
short hr = BitConverter.ToInt16(data, offset);
offset += 2;
System.Diagnostics.Debug.WriteLine("We have 16:" + hr.ToString("x"));
}
else // if BPM is uint8
{
byte hr = data[offset];
offset += 1;
System.Diagnostics.Debug.WriteLine("no 16:" + hr.ToString("x"));
}
bool ee = (flags & (1 << 3)) != 0;
if (ee)
offset += 2;
// bool rr = ((flags & 1 << 4) != 0);
bool rr = ((flags & 0x10) != 0);
if (rr)
{
int count = (data.Length - offset) / 2;
for (int i = 0; i < count; i++)
{
ushort value = BitConverter.ToUInt16(data, offset);
intervals.Add((double)value); // Added
if (intervals.Count > 190) // Added
intervals.RemoveAt(0);// Added
double mean = intervals.Average();// Added
double sumOfSquareDiff = intervals.Select(val => (val - mean) * (val - mean)).Sum(); // Added
double vrHR = Math.Sqrt(sumOfSquareDiff / intervals.Count); // Added
double intervalLengthInSeconds = value / 1024.0;
offset += 2;
System.Diagnostics.Debug.WriteLine("Heart Rate Variability:" + vrHR.ToString());
}
}
And it's printing the output like this.
But I want the Heart Rate Variability to print just below "Value at".
How do I make that work ?
Should I do pass by value or reference? Any other suggestions ?
I asked more detailed question earlier here on Stack Overflow
But I want the Heart Rate Variability to print just below "Value at". How do I make that work ?
Your question completely was unrelated to 'pass by value or reference'. The CharacteristicLatestValue just is a TextBlock control on XAML page. It's used to show text on UI like the following:
Value at 01:11:25:453: Heart Rate: 124
If you want to show 'no 16:51', 'Heart Rate Varibility:661841865028902' etc these texts blow it like the following:
Value at 01:11:25:453: Heart Rate: 124
no 16:51
Heart Rate Varibility:661841865028902
You just need to add them after the CharacteristicLatestValue.Text like the following:
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() => CharacteristicLatestValue.Text = "Value at 01:11:25:453: Heart Rate: 124"+"\r\n"+ "no 16:51"+"\r\n"+ "Heart Rate Varibility:661841865028902"+"\r\n");

System Overflow Exception in System.Windows.Forms.DataVisualization.dll

I've got problem that makes me crazy - I think that's so easy that I can't even think about what is causing my problems.
I'm sending data (fft of generated acustic wave - frequencies and magnitude) generated by my uC to PC by serialport.
Everything seems to work fine till I try to chart my data with MS Chart Controls - without charting there are no errors when recieving data.
I'm always able to chart data once or twice, after that I'm getting error like this:
An unhandled exception of type 'System.OverflowException' occurred in
System.Windows.Forms.DataVisualization.dll
Additional information: Value was either too large or too small for a
Decimal.
I checked if vaules of data are over limits of "float32" aka Single - I even applied statement where I make values lower when they are too high - it's useless. The part of my with recievedata event and charting is like this:
private byte[] bArray = new byte[20 * numberOfSamples + 4]; // frequencies (512 samples) and magnitudes (512 values) - each is a single precision float so 4 bytes + 4 - "!!!!" at the beginning
private float[] fArray = new float[2 * numberOfSamples + 1]; // float data array
private void SerialP_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
int previouse = counterOfRecBytes;
counterOfRecBytes += sp.BytesToRead;
//if (counterOfRecBytes >= 4104)
sp.Read(bArray, previouse, (counterOfRecBytes - previouse));
if (counterOfRecBytes >= 8 * numberOfSamples + 4)
{
for (uint i = 0; i < 2 * numberOfSamples + 1; i++)
{
fArray[i] = Math.Abs(ByteToFloat(bArray, i));
if (fArray[i] < 0) fArray[i] = 0;
if (fArray[i] > 3.4 * Math.Pow(10, 38) || fArray[i] < -3.4 * Math.Pow(10, 38)) fArray[i] = 0;
}
if (InvokeRequired)
{
Invoke(new MethodInvoker(ChartData));
}
else
{
ChartData();
}
// Set counterOfRecBytes to recieve new data
counterOfRecBytes = 0;
}
}
///// <summary>
///// Changes my recieved bArray to single precision floats
///// </summary>
private float ByteToFloat(byte[] input, UInt32 i)
{
byte[] array = new[] { input[4 * i], input[4 * i + 1], input[4 * i + 2], input[4 * i + 3] };
return BitConverter.ToSingle(array, 0);
}
/// <summary>
/// Setting chart data
/// </summary>
private void ChartData()
{
chart1.Series["Widmo"].Points.Clear();
for(int i = 1; i < numberOfSamples + 1; i++)
{
chart1.Series["Widmo"].Points.AddXY(fArray[i], fArray[i + numberOfSamples]);
}
}

error when trying to read from an array

I am trying too have my C# console application make a beep sound. Yes I know I can use Console.Beep but I also want to lower the volume etc.
But the error I am getting is this:
Method name expected
on this line:
binaryWriter.Write(hdr(i));
This is my code:
private bool Beep(int volume, int frequency, int duration)
{
try
{
double amplitude = volume * 1.27;
double a = ((amplitude * (System.Math.Pow(2, 15))) / 1000) - 1;
double deltaFt = 2 * System.Math.PI * frequency / 8000;
double samples = 441 * (duration / 100);
int bytes = Convert.ToInt32(samples) * 4;
int[] hdr = {
0x46464952,
36 + bytes,
0x45564157,
0x20746d66,
16,
0x20001,
8000,
176400,
0x100004,
0x61746164,
bytes
};
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(44 + bytes))
{
using (System.IO.BinaryWriter binaryWriter = new System.IO.BinaryWriter(memoryStream))
{
for (int i = 0; i <= hdr.Length - 1; i++)
{
binaryWriter.Write(hdr(i));
}
for (int T = 0; T <= Convert.ToInt32(samples) - 1; T++)
{
short sample = Convert.ToInt16(a * System.Math.Sin(deltaFt * T));
binaryWriter.Write(sample);
binaryWriter.Write(sample);
}
binaryWriter.Flush();
memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
using (System.Media.SoundPlayer sp = new System.Media.SoundPlayer(memoryStream))
{
sp.PlaySync();
}
}
}
}
catch
{
return false;
}
return true;
}
Your hdr is an array, you need to get the entry by putting the square brackets and then passing the index
binaryWriter.Write(hdr[i]);

C# .wav file processing advice needed

What I am making is a small audio editor that loads a .wav file and displays it in the time domain. The user can select a part of it and zoom in or DFT the chunk which displays a small window in the frequency domain. (extra functionality to be added later)
I think I am making a mistake when splitting my byte array into two float arrays.
I use NAudio to get my samples into a byte array.
Then I use a loop I found on stack overflow to split the array into left and right channels.
private void readToArrays(WaveFileReader pcm) {
wf = pcm.WaveFormat;
int samplesDesired = (int)pcm.Length;
buffer = new byte[samplesDesired * 4];
left = new float[samplesDesired];
right = new float[samplesDesired];
int bytesRead = pcm.Read(buffer, 0, samplesDesired);
if (wf.BitsPerSample == 16) {
if (wf.Channels == 1) {
for (int i = 0; i < buffer.Length / 4; i++)
//handle
}
else if (wf.Channels == 2) {
int index = 0;
for (int sample = 0; sample < bytesRead / 4; sample++) {
left[sample] = BitConverter.ToInt16(buffer, index);
index += 2;
right[sample] = BitConverter.ToInt16(buffer, index);
index += 2;
}
}
}else if (wf.BitsPerSample == 8) {
if (wf.Channels == 1) {
//handle
}
else if (wf.Channels == 2) {
//handle
}
}
}
TL:DR: I get a lot of noise from playback with individual files. What I need is some advice as to how I can still be able to modify my samples / DFT them AND be able to output them without noise. Do note that I am just a 2nd year Computing student and do not have years of experience.
Extra Info : Bit Depth = 16; Sample Rate = 22050;

Detecting audio silence in WAV files using C#

I'm tasked with building a .NET client app to detect silence in a WAV files.
Is this possible with the built-in Windows APIs? Or alternately, any good libraries out there to help with this?
Audio analysis is a difficult thing requiring a lot of complex math (think Fourier Transforms). The question you have to ask is "what is silence". If the audio that you are trying to edit is captured from an analog source, the chances are that there isn't any silence... they will only be areas of soft noise (line hum, ambient background noise, etc).
All that said, an algorithm that should work would be to determine a minimum volume (amplitude) threshold and duration (say, <10dbA for more than 2 seconds) and then simply do a volume analysis of the waveform looking for areas that meet this criteria (with perhaps some filters for millisecond spikes). I've never written this in C#, but this CodeProject article looks interesting; it describes C# code to draw a waveform... that is the same kind of code which could be used to do other amplitude analysis.
If you want to efficiently calculate the average power over a sliding window: square each sample, then add it to a running total. Subtract the squared value from N samples previous. Then move to the next step. This is the simplest form of a CIC Filter. Parseval's Theorem tells us that this power calculation is applicable to both time and frequency domains.
Also you may want to add Hysteresis to the system to avoid switching on&off rapidly when power level is dancing about the threshold level.
http://www.codeproject.com/Articles/19590/WAVE-File-Processor-in-C
This has all the code necessary to strip silence, and mix wave files.
Enjoy.
I'm using NAudio, and I wanted to detect the silence in audio files so I can either report or truncate.
After a lot of research, I came up with this basic implementation. So, I wrote an extension method for the AudioFileReader class which returns the silence duration at the start/end of the file, or starting from a specific position.
Here:
static class AudioFileReaderExt
{
public enum SilenceLocation { Start, End }
private static bool IsSilence(float amplitude, sbyte threshold)
{
double dB = 20 * Math.Log10(Math.Abs(amplitude));
return dB < threshold;
}
public static TimeSpan GetSilenceDuration(this AudioFileReader reader,
SilenceLocation location,
sbyte silenceThreshold = -40)
{
int counter = 0;
bool volumeFound = false;
bool eof = false;
long oldPosition = reader.Position;
var buffer = new float[reader.WaveFormat.SampleRate * 4];
while (!volumeFound && !eof)
{
int samplesRead = reader.Read(buffer, 0, buffer.Length);
if (samplesRead == 0)
eof = true;
for (int n = 0; n < samplesRead; n++)
{
if (IsSilence(buffer[n], silenceThreshold))
{
counter++;
}
else
{
if (location == SilenceLocation.Start)
{
volumeFound = true;
break;
}
else if (location == SilenceLocation.End)
{
counter = 0;
}
}
}
}
// reset position
reader.Position = oldPosition;
double silenceSamples = (double)counter / reader.WaveFormat.Channels;
double silenceDuration = (silenceSamples / reader.WaveFormat.SampleRate) * 1000;
return TimeSpan.FromMilliseconds(silenceDuration);
}
}
This will accept almost any audio file format not just WAV.
Usage:
using (AudioFileReader reader = new AudioFileReader(filePath))
{
TimeSpan duration = reader.GetSilenceDuration(AudioFileReaderExt.SilenceLocation.Start);
Console.WriteLine(duration.TotalMilliseconds);
}
References:
How audio dB levels are calculated.
Floating-point samples range.
More about amplitude.
Here a nice variant to detect threshold alternatings:
static class AudioFileReaderExt
{
private static bool IsSilence(float amplitude, sbyte threshold)
{
double dB = 20 * Math.Log10(Math.Abs(amplitude));
return dB < threshold;
}
private static bool IsBeep(float amplitude, sbyte threshold)
{
double dB = 20 * Math.Log10(Math.Abs(amplitude));
return dB > threshold;
}
public static double GetBeepDuration(this AudioFileReader reader,
double StartPosition, sbyte silenceThreshold = -40)
{
int counter = 0;
bool eof = false;
int initial = (int)(StartPosition * reader.WaveFormat.Channels * reader.WaveFormat.SampleRate / 1000);
if (initial > reader.Length) return -1;
reader.Position = initial;
var buffer = new float[reader.WaveFormat.SampleRate * 4];
while (!eof)
{
int samplesRead = reader.Read(buffer, 0, buffer.Length);
if (samplesRead == 0)
eof = true;
for (int n = initial; n < samplesRead; n++)
{
if (IsBeep(buffer[n], silenceThreshold))
{
counter++;
}
else
{
eof=true; break;
}
}
}
double silenceSamples = (double)counter / reader.WaveFormat.Channels;
double silenceDuration = (silenceSamples / reader.WaveFormat.SampleRate) * 1000;
return TimeSpan.FromMilliseconds(silenceDuration).TotalMilliseconds;
}
public static double GetSilenceDuration(this AudioFileReader reader,
double StartPosition, sbyte silenceThreshold = -40)
{
int counter = 0;
bool eof = false;
int initial = (int)(StartPosition * reader.WaveFormat.Channels * reader.WaveFormat.SampleRate / 1000);
if (initial > reader.Length) return -1;
reader.Position = initial;
var buffer = new float[reader.WaveFormat.SampleRate * 4];
while (!eof)
{
int samplesRead = reader.Read(buffer, 0, buffer.Length);
if (samplesRead == 0)
eof=true;
for (int n = initial; n < samplesRead; n++)
{
if (IsSilence(buffer[n], silenceThreshold))
{
counter++;
}
else
{
eof=true; break;
}
}
}
double silenceSamples = (double)counter / reader.WaveFormat.Channels;
double silenceDuration = (silenceSamples / reader.WaveFormat.SampleRate) * 1000;
return TimeSpan.FromMilliseconds(silenceDuration).TotalMilliseconds;
}
}
Main usage:
using (AudioFileReader reader = new AudioFileReader("test.wav"))
{
double duratioff = 1;
double duration = 1;
double position = 1;
while (duratioff >-1 && duration >-1)
{
duration = reader.GetBeepDuration(position);
Console.WriteLine(duration);
position = position + duration;
duratioff = reader.GetSilenceDuration(position);
Console.WriteLine(-duratioff);
position = position + duratioff;
}
}
I don't think you'll find any built-in APIs for detection of silence. But you can always use good ol' math/discreete signal processing to find out loudness.
Here's a small example: http://msdn.microsoft.com/en-us/magazine/cc163341.aspx
Use Sox. It can remove leading and trailing silences, but you'll have to call it as an exe from your app.
See code below from Detecting audio silence in WAV files using C#
private static void SkipSilent(string fileName, short silentLevel)
{
WaveReader wr = new WaveReader(File.OpenRead(fileName));
IntPtr format = wr.ReadFormat();
WaveWriter ww = new WaveWriter(File.Create(fileName + ".wav"),
AudioCompressionManager.FormatBytes(format));
int i = 0;
while (true)
{
byte[] data = wr.ReadData(i, 1);
if (data.Length == 0)
{
break;
}
if (!AudioCompressionManager.CheckSilent(format, data, silentLevel))
{
ww.WriteData(data);
}
}
ww.Close();
wr.Close();
}

Categories