trying to output a signal from csv file

Hi,
I am not a good programmer, but for hardware testing purposes I need to have a board output a waveform from a .csv file. It is a short file that would need to be repeated, which I can do, but I'm not sure where to start.

The file is the output from a sensor sampled at 256Hz, and no signal amplification is needed. Can anyone help me get started?
drehl wrote:
The (csv) file is the output from a sensor...


Fine. I understand that. You also say:

drehl wrote:
I need to have a board output a waveform from a .csv file.


I understand that too, I think. I conclude from both that the file is the output of some sensor sampling process (which is pretty much done and working) and now you need assistance on how to produce sound based on the data recorded in the CSV. Correct?

If I am, I guess you need to translate this to a sound file format appropriate to the sound card installed in the PC and the operating system installed in it. BUT, you also mentioned "a board". Does this mean that you have installed propietary hardware to the PC and you want the waveform to output through this hardware of yours? If so, the answer is Windows is: Create a driver for the board.

Do I know how to create drivers? No. :-( Plus, I don't know the answer if you are in a different operating system.

So now that I think I have provided with what you need to do, my question would be: Where in this process do you need help? Reading the data from the CSV file into memory? Using that data and producing a .wav file or other sound file? Writing the driver? Something else?
A csv usually contains data in 2 dimensions. The columns are separated by commas and the rows are separated by the '\n' character. I'm going to assume that your data only has one dimension, seperated by the '\n' character.

You'll read the file like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <Windows.h>
using namespace std;

vector<double> data;

void readFile()
{
	string Line;
	ifstream fin("intput.csv");
	while(getline(fin,Line))
	{
		data.push_back( atof(Line.c_str())); //Converts the string to a double and puts that into the data array.
	}
}


This will load your CSV data into a vector or array. Then you will want to output your result. I'm guessing that you have an IO available that sets a voltage or something asynchronously using a label. Well, then you just need to set that label to each piece of data in the vector and ensure that it keeps its value for the desired amount of time to achieve 256Hz. We can sort of do this with the windows Sleep() command

1
2
3
4
5
6
7
8
void outputData()
{
	for (vector<double>::iterator it = data.begin(); it < data.end(); it++)
	{
		VoltageOut = *it;
		Sleep(4);
	}
}


The only problem is that this will run at ~250Hz. The reason for this is that the Sleep() command only gives us milisecond precision. Therefore an iteration period of 4ms will give us 250Hz. To get better resolution you will need to install something like RTX.

Don't forget that most voltage converters take an analogue integer value so ensure that this is calibrated to your floating point number.

If you want to stick this togeather just add this below:
1
2
3
4
5
6
7
8
9
int main()
{
    readFile(); //Read the file first.
    while(true)
    {
        outputData(); //Repeat the output until the process is terminated.
    }
    return 0;
}
Last edited on
A note on Stewbond's code: Sleep() is no good solution. In reality, Sleep() can only sleep for about 10 milliseconds the shortest, but it is usually some 15 milliseconds. This is a limitation of the operating system that has to do with yielding processor slice time and being rescheduled.

The art of outputting sound (I imagine) requires much more precise mechanisms. A decent sound card probably has specialized hardware to keep the timing in perfect harmony. Most likely, sound drivers accept an entire buffer with data that is passed along to the sound board as a whole where the sound card can easily plug into its time-keeping mechanism.
I didn't even think of sound applications. I was thinking more along the lines of vibration analysis or electrical measurements. I see how sound would be a nice wrapper for this.
Stewbond seems to have the right idea, sorry for not elaborating. it is an accelerometer. the test is for 7 days worth of data to be fed into a device to record and analyze it. it is actually a two column csv, but what you gave me should be enough to get me started.
Thanks for the help.
Topic archived. No new replies allowed.