Adding sine curves to obtain a complex 1D signal.

The following code demonstrates how different sine curves can be added to obtain a complex signal.

demoSine(){
	const int maxTime = 300;
	const int numSignals = 5;
	float phase[numSignals] = {0.06, 0.1, 0.2, 0.4, 0.55};
	float amplitude[numSignals] = {50, 40, 10, 30, 20};
	Scalar colors[numSignals] = {Scalar(255,0,0), Scalar(180,170,255), Scalar(0,0,255), Scalar(0,255,0), Scalar(255,0,255)};

	double y[numSignals][maxTime];
	double allAdded[numSignals][maxTime];
	//int yAxisLocation = 100;
	int imageColumns = maxTime;

	int minY = 1, maxY = -1;

	for (int i=0; i< numSignals; i++)
		for(int x=0;x< maxTime;x++){
			double value = amplitude[i] * sin(((float)x) * phase[i]);

			y[i][x]=value;

			if (i == 1){
				value = y[0][x] + y[1][x];
				allAdded[0][x] = value;
			}

			if (i >= 2){
				value = allAdded[i-2][x] + y[i][x];
				allAdded[i-1][x] = value;
			}
			if (minY > value)
				minY = value;
			if (maxY < value)
				maxY = value;

			//LogManager::writePrintfToLog(LogManager::ExactStatus, "CorrelationDemo::demoFilters", " %2.3f", value);
		}

	LogManager::writePrintfToLog(LogManager::ExactStatus,
			"CorrelationDemo::demoFilters", " minY=%d, maxY=%d", minY, maxY);

	int offset = 50;
	int imageRows = maxY - minY + offset * 2;

	if (imageRows < 200)
		imageRows = 300;

	if (imageRows > 2000){
		std::stringstream errMsg;
		errMsg < < "Image size is too large" << imageRows;
		throw std::runtime_error(errMsg.str());
	}
	int offsetYForPlotting = imageRows / 2 - (offset / 2);

	/*int imageRows = 500;

	int offsetYForPlotting = imageRows / 2;*/

	for (int i=0; i< numSignals; i++){
		Mat outImage(imageColumns, imageRows, CV_8UC3, Scalar(255, 255, 255));
		line(outImage ,
			Point(0, offsetYForPlotting),
			Point(maxTime-1, offsetYForPlotting),
			Scalar(0,0,0),
			1, 4, 0);

		for(int x=0;x< maxTime-1;x++)
		{
			line(outImage ,  		// the dest image
				Point(x, offsetYForPlotting + y[i][x]),  	/* start point */
				Point(x+1, offsetYForPlotting + y[i][x+1]), /* end point */
				colors[i],  /* the color */
				2, 4, 0);           /* thickness, line type, shift */
		}
		char outFileName[256];
		sprintf(outFileName, "/tmp/sineDemo.%d.png", i);
		imwrite(outFileName, outImage);
	}

	for (int i=0; i < numSignals; i++){
		Mat outImage(imageColumns, imageColumns, CV_8UC3, Scalar(255, 255, 255));
		line(outImage ,
			Point(0, offsetYForPlotting),
			Point(maxTime-1, offsetYForPlotting),
			Scalar(0,0,0),
			1, 4, 0);

		for(int x=0;x < maxTime-1;x++)
		{
			line(outImage ,  		// the dest image
				Point(x, offsetYForPlotting + allAdded[i][x]),  	/* start point */
				Point(x+1, offsetYForPlotting + allAdded[i][x+1]), /* end point */
				colors[i],  /* the color */
				2, 4, 0);           /* thickness, line type, shift */
		}
		char outFileName[256];
		sprintf(outFileName, "/tmp/addedSineDemo.%d.png", i);
		imwrite(outFileName, outImage);
	}
}

Comments

Popular posts from this blog

Discrete Wavelet Transform

Geometric image transforms

Testing image capture with high speed cameras