Plot only part of QwtCurve in real time plot - c++

I have a QVector in which I'm constantly appending data so I can plot on a QwtPlot. But with a high frequency I guess the vector becames too large and the program crashes.
My question is, how can I create a QwtCurve which is only beggining in some point of time, bacause the time that has already passed is not necessary in the vector as it was already plotted.
here's my code:
QVector<double> xv;
QVector<double> cv1;
QVector<double> cv2;
as global variables,
void gui::process_new_info(QByteArray array)
{
int v = 0;
double f = ui->frequency->value();
xv.append(sizeof_array/f);
char *buf = array.data();
if (ui->ecgPlux->isChecked() == true || ui->channel_1->currentIndex() != 0)
{
cv1.append(buf[1]);
QwtPlotCurve *curve1 = new QwtPlotCurve;
curve1->attach(plot_all[0]);
curve1->setData(xv,cv1);
curve1->setPen(QPen(Qt::blue,1));
plot_all[0]->replot();
QwtPlotCurve *curve2 = new QwtPlotCurve;
curve2->attach(plot[0]);
curve2->setData(xv,cv1);
curve2->setPen(QPen(Qt::blue,1));
plot[0]->replot();
}
if (ui->xyzPlux->isChecked() == true || ui->channel_2->currentIndex() != 0)
{
cv2.append(buf[2]);
QwtPlotCurve *curve3 = new QwtPlotCurve;
curve3->attach(plot_all[1]);
curve3->setData(xv,cv2);
curve3->setPen(QPen(Qt::blue,1));
plot_all[0]->replot();
QwtPlotCurve *curve4 = new QwtPlotCurve;
curve4->attach(plot[1]);
curve4->setData(xv,cv1);
curve4->setPen(QPen(Qt::blue,1));
plot[1]->replot();
}
//printf ("%d ->", buf[0]);
fprintf (data, "%d,", buf[0]);
for (int i = 1; i < 9; i++)
{
v = buf[i];
//printf ("%d,", v);
fprintf (data, "%d,", v);
}
//printf ("\n");
fprintf (data, "\n");
sizeof_array++;
}

QwtPlotCurve
http://qwt.sourceforge.net/class_qwt_plot_curve.html
inherits from QwtPlotSeriesItem
http://qwt.sourceforge.net/class_qwt_plot_series_item.html
There is a warning in setData
The item takes ownership of the data object, deleting it when its not used anymore.
It probably isn't the fact that you are growing too big... it may be that you are accessing something that Qwt deleted.
Run it in a debugger, and look at the stack trace for where it died, or put in a bunch of qDebug() lines to see where it is dying.
If it is something with the data being too large, you could pop off items off of the head of your vector, before setting the vector.
Hope that helps.

Related

How does AudioKit's AKNodeOutputPlot pull it's data?

I'm very new to the AudioKit framework and I have been trying to understand a bit more about the DSP side to it. Whilst rummaging around in the source code I realised that AKNodeOutputPlot does not pull data from the node the same way others would.
In the DSP code for the AKAmplitudeTracker an RMS value is calculated for each channel and the result is briefly written to the output buffer but at the end of the for loop the node is essentially bypassed by setting the output to the original input:
void process(AUAudioFrameCount frameCount, AUAudioFrameCount bufferOffset) override {
for (int frameIndex = 0; frameIndex < frameCount; ++frameIndex) {
int frameOffset = int(frameIndex + bufferOffset);
for (int channel = 0; channel < channels; ++channel) {
float *in = (float *)inBufferListPtr->mBuffers[channel].mData + frameOffset;
float temp = *in;
float *out = (float *)outBufferListPtr->mBuffers[channel].mData + frameOffset;
if (channel == 0) {
if (started) {
sp_rms_compute(sp, leftRMS, in, out);
leftAmplitude = *out;
} else {
leftAmplitude = 0;
}
} else {
if (started) {
sp_rms_compute(sp, rightRMS, in, out);
rightAmplitude = *out;
} else {
rightAmplitude = 0;
}
}
*out = temp;
}
}
}
This makes sense since outputting the RMS value to the device speakers would sound terrible but when this node is used as the input to the AKNodeOutputPlot object RMS values are plotted.
I assumed that the leftAmplitude and rightAmplitude variables were being referenced somewhere but even if they are zeroed out the plot works just fine. I'm interested in doing some work on the signal without effecting the output so I'd love it someone could help me figure how the AKPlot is grabbing this data.
Cheers
AKNodeOutputPlot works with something called a "tap":
https://github.com/AudioKit/AudioKit/blob/master/AudioKit/Common/User%20Interface/AKNodeOutputPlot.swift
There are also a few other taps that are not necessarily just for user interface purposes:
https://github.com/AudioKit/AudioKit/tree/master/AudioKit/Common/Taps
Taps allow you to inspect the data being pulled through another node without being inserted into the signal chain itself.

multiple lists into a map

I had a question regarding my code below. I'm reading a file containing lots of data of which some stuff is irrelevant. The data is written out on one line, so I cannot use nextLine or something.
For each vertex, I save the relevant information into dataperpoint. When I go to the next vertex, I want to clear the list to fill it with new relevant information.
The issue that I have is that each time I clear dataperpoint, all values in Map get cleared. When I then try to fill it, all previous positions in the Map get the same values.
How can I do this and make sure that each vertex will get his own list?
Looking forward to your suggestions!
public static Map<Integer, List<Double>> readData(File f) // throws IO exception?
{
// Create Map to store the vertex and list with relevant information in
List<Double> dataperpoint = new ArrayList<Double>();
Map<Integer, List<Double>> data = new HashMap<>();
// Open the scanner
try (Scanner in = new Scanner(f))
{
// To make sure the correct localization is used
in.useLocale(Locale.US);
// The first six integers contain irrelevant information
for (int step = 1; step <= 6; step++)
{
in.nextInt();
}
// Do the information for vertex 0 separately, since it has one data point less
int vertex = in.nextInt();
for (int doubleinfo = 1; doubleinfo <= 4; doubleinfo++) // six relevant variables
{
dataperpoint.add(in.nextDouble());
}
// irrelevant information
for (int irrelevantinfo = 1; irrelevantinfo <= 2; irrelevantinfo++)
{
in.nextInt();
}
// Opening and Closing of time window
dataperpoint.add((double) in.nextInt());
dataperpoint.add((double) in.nextInt());
data.put(vertex, dataperpoint);
while (in.hasNext()) // think of different statement later
{
dataperpoint = new ArrayList<Double>();
vertex = in.nextInt();
for (int doubleinfo = 1; doubleinfo <= 4; doubleinfo++) // six relevant variables
{
dataperpoint.add(in.nextDouble());
}
// irrelevant information
for (int irrelevantinfo = 1; irrelevantinfo <= 3; irrelevantinfo++)
{
in.nextInt();
}
// Opening and Closing of time window
dataperpoint.add((double) in.nextInt());
dataperpoint.add((double) in.nextInt());
data.put(vertex, dataperpoint);
}
in.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
Use LinkedHashMap<> instead of HashMap<> it should solve your problem. Read this Difference between HashMap, LinkedHashMap and TreeMap

ROOT(cern) FILE reading+TGraph works ok, but TGraphErrors doesnt want

The problem is next:
I had read my data from a file to the array. Graph plotting works without any problem. But TGraphErrors doesnt want to do anything. Root says
Error: Can't call
TGraphErrors::SetPoint(i,Data[0][i],Data[1][i],Data[2][i],Data[2][i])
in current scope plot2.C:85: Possible candidates are... (in
TGraphErrors) (in TGraph)
/usr/local/Cellar/root/5.34.26/lib/root/libHist.so -1:-1 0 public:
virtual void TGraph::SetPoint(Int_t i,Double_t x,Double_t y);
* Interpreter error recovered *"
Code is below:
void plot2(char* fin1){
const int VAR_N=20,NPOINTS=10000;
Double_t Data[VAR_N][NPOINTS];
int counts,N1;
float E,E1,dE;
FILE* fin12;
printf("%s \n",fin1);
fin12 = fopen(fin1,"r");
if (fin12 == NULL){
printf("Can't open input file: %s !\n",fin1);
}
else{
counts=0;
while (!feof(fin12)) {
fscanf(fin12, "%f %f %f \n",&E,&E1,&dE);
printf("%f %f %f \n",E,E1,dE);
Data[0][counts] = E;
Data[1][counts] = E1;
Data[2][counts] = dE; }
N1=counts;
printf("NP1=%d ",N1);
fclose(fin12);
}
TGraph* V_Graph = new TGraph(N1);
TGraphErrors* V_GraphErrors = new TGraphErrors(N1);
for(int i=0;i<N1;i++){
V_Graph->SetPoint(i,Data[0][i],Data[1][i]);
}
for(int i=0;i<N1;i++){
V_GraphErrors->SetPoint(i,Data[0][i],Data[1][i],Data[2][i],Data[2][i]);
}
TCanvas *c1 = new TCanvas("c1", "c1", 1200, 1000);
c1->cd(1);
TString VName="+Errors";
V_Graph->SetTitle(VName);
V_Graph->SetMarkerStyle(21);
V_Graph->SetMarkerSize(1);
V_Graph->SetMarkerColor(kBlue);
V_Graph->GetXaxis()->SetTitle("x");
V_Graph->GetYaxis()->SetTitle("y");
V_Graph->GetXaxis()->SetLimits(4.,25.);
V_Graph->GetYaxis()->SetRangeUser(10.,80.);
V_Graph->Draw("APL");
V_GraphErrors->SetMarkerStyle(21);
V_GraphErrors->SetMarkerSize(1);
V_GraphErrors->SetMarkerColor(kGreen);
//V_Graph->GetXaxis()->SetLimits(4.,11.);
//V_Graph->GetYaxis()->SetRangeUser(45.,80.);
V_GraphErrors->Draw("C*");
}
I'm new in root-using.Probably i did smthg stupid and didnt notice it. So what i did wrong?
Thank you in advance
Olka
The problem is that there is no SetPointError method with the signature you are trying to use (int, Double_t, Double_t, Double_t, Double_t). To set the errors you should use the SetPointError(Double_t ex, Double_t ey) method. For example, in your code above you would do something like:
V_GraphErrors->SetPoint(i,Data[0][i],Data[1][i]]);
V_GraphErrors->SetPointError(i,Data[2][i],Data[2][i]);
Although it would probably be easier to pass the arrys directly in the constructor:
V_GraphErrors(N1, Data[0], Data[1], Data[2], Data[2]);
Btw, this will set the x and y errors to be identical. I'm not sure if this is what you intended.
See https://root.cern.ch/root/html/TGraphErrors.html for details of the methods available in TGraphErrors

Creating new objects in C++ function causes program to crash

I have a program which allows the user to play Dominoes against 3 CPU players, with varying difficulty. Each CPU player can be either Beginner, Intermediate or Expert, and each difficulty has it's own class. If I initiate my 3 CPU players at the beginning of my 'Window' class (below), the program runs fine.
In Window.h
public:
Window(QWidget *parent = 0);
Intermediate *cpu1;
Beginner *cpu2;
Intermediate *cpu3;
In Window.cpp
Window::Window(QWidget *parent):QDialog(parent) {
cpu1 = new Intermediate;
cpu2 = new Beginner;
cpu3 = new Intermediate;
}
However I want the user to be able to select the CPU difficulties at the beginning of the game, so I now have a function within 'Window' that creates the objects. As soon as I call this function the game freezes and I get an error message pop up saying telling me the program has ended unexpectedly.
void Window:: startGame(){
cpu1 = new Intermediate;
cpu2 = new Beginner;
cpu3 = new Intermediate;
}
If anyone would be able to explain to me what is going on and what I can do to get around this that would be great.
Intermediate.cpp (Beginner.cpp is almost identical)
#include "intermediate.h"
Intermediate::Intermediate()
{
tilePlaced = false;
skipGo = false;
}
void Intermediate::findDoubles(int a[7][2]){
for(int i = 0; i < 7; i++){ // Creates new doubles list after each go.
doublesList[i] = 0;
}
for(int i = 0; i < 7; i++){ // Creates a list of doubles
if ((a[i][0] == a[i][1]) && (a[i][0] != 7)){
doublesList[a[i][0]] = 1;
}
}
}
bool Intermediate::addDomino(){} // Function that finds best domino to replace and returns bool
if(tilePlaced == false){
pass++;
text += "\nPassed turn";
return false;
}
else{
pass = 0;
text += QString("\nPlaced [%1 : %2]").arg(a).arg(b);
return true;
}
}
One way to start would be to narrow down which class is causing the fault. Does it work if they are all Beginner, or if they are all Intermediate? If so then the other one is causing the problem.

Creating object in C++ , what if already constructed?

I am still new to c++. I want to read in messages from several sources. Each source will begin data messages with a 4 char ID. Each will also have several data messages. No one message has all of the info I want from the device. So if I create an object with the ID as the object name, the next time a message is received, will the object be updated or completely reconstructed? Is there a way to check if the object is already constructed before calling it in the code?
class Channels{
public:
INT8U systemID; //0x01 Glonass, 0x02 GPS
INT8U satID;
INT8U GlonassNumber;
INT8U SNR; //signal to noise ratio
FP64 carrierPhase; //cylces
FP64 psuedoRange; //milliseconds
FP64 doppler; //HZ cycles
float tropDelay; //meters
float ionoDelay; //meters
};
class BaseStation{
public:
Channels channel[32]; //each channel object has all channel class variables in it
int numberSatelitesTracked;
FP64 timeUTC;
INT16U week;
FP64 GPStoUTCoffset;
FP64 GLOtoUTCoffset;
INT8S recieverTimeOffset;
FP64 posX; //geocentric coordinates in meters
FP64 posY;
FP64 posZ;
FP64 rmsX; //expected root mean square error of coordinates
FP64 rmsY;
FP64 rmsZ;
};
if( check == SOCKET_ERROR){
if( WSAGetLastError() != WSAEWOULDBLOCK){
printf("base station client recieve failed with error %d \n", WSAGetLastError());
FreeSocketInformation(i); //shuts down client socket if no data
}
continue;
}
else{
//recieve bytes into array
memcpy(recvArray, SocketInfo->DataBuf.buf, SocketInfo->RecvBytes +1);
//print recieved bytes on screen
printf("%s \n", SocketInfo->DataBuf.buf);
//first 4 bytes in message are base ID
cBuffer[0] = recvArray[0];
cBuffer[1] = recvArray[1];
cBuffer[2] = recvArray[2];
cBuffer[3] = recvArray[3];
baseID = cBuffer;
//create object with 4 char name
BaseStation baseID;
//test message identity and sort data
if(recvArray[4] == 0x10 && recvArray[5] == 0xF5){
baseID.timeUTC = combine64(recvArray[6]);
baseID.week = combine16u(recvArray[14]);
baseID.GPStoUTCoffset = combine64(recvArray[16]);
baseID.GLOtoUTCoffset = combine64(recvArray[24]);
baseID.recieverTimeOffset = recvArray[32];
int noChannels = (check-30) /30 ;
if (noChannels >= 32){
noChannels = 32;
}
int x = 33;
for(int m = 0; m < noChannels; m++){ //advance reading for channel m
baseID.channel[m].systemID = recvArray[x];
x++;
baseID.channel[m].satID = recvArray[x];
x++;
baseID.channel[m].GlonassNumber = recvArray[x];
x++;
baseID.channel[m].SNR = recvArray[x];
x++;
baseID.channel[m].carrierPhase = combine64(recvArray[x]);
x = x+8;
baseID.channel[m].psuedoRange = combine64(recvArray[x]);
x = x+8;
baseID.channel[m].doppler = combine64(recvArray[x]);
x = x+10;
} //end of for loop to gather F5 sat data
} //end F5 message data
if(recvArray[4] == 0x10 && recvArray[5] == 0xF6){
baseID.posX = combine64(recvArray[6]);
baseID.posY = combine64(recvArray[14]);
baseID.posZ = combine64(recvArray[22]);
baseID.rmsX = combine64(recvArray[30]);
baseID.rmsY = combine64(recvArray[38]);
baseID.rmsZ = combine64(recvArray[46]);
} //end F6 message data
OK so it seems an Array may be the best for me to use. So if I setup 100 base objects and then track the active array elements with a second boolean array, does this look like it should work? (baseID added to the base object)
BaseStation base[100];
boolean baseActive[100];
int baseNumber;
//begin message processing------------------------------------------------------------
//first 4 bytes in message are base ID
cBuffer[0] = recvArray[0];
cBuffer[1] = recvArray[1];
cBuffer[2] = recvArray[2];
cBuffer[3] = recvArray[3];
string name = cBuffer;
//check for existing baseID------------------------------------------------------------
// 100 array positions
//find if base is already in use, create new if not in use
for(baseNumber = 0; base[baseNumber].baseID != name; baseNumber++){
//for statement increases untill it finds baseID == name
if( baseNumber >= 100){ //baseID not currently in use
for(int n=0; baseActive[n] == true; n++){
//for statement increases untill finds a false baseActive
baseNumber = n; //assign baseNumber to the array position
base[baseNumber].baseID = name; //create new baseID
continue;
}
}
}
//check and process message data--------------------------------------------------------
if( base[baseNumber].baseID == name){
baseActive[baseNumber] = true;
//test message identity and sort data
}//end of for loop
//test connection, if no bytes recieved then connection is closed.----------------------
if( SocketInfo->RecvBytes == 0){
FreeSocketInformation(i); //shuts down client socket if no data
continue;
}
}
} //end of read data from socket
}
//need to add a timer to remove non sending bases from the baseActive[] array
C++ is a statically typed language, You need to provide the object name at compile time.
You cannot create an object name at run-time and create object with that name.
As already answered, you can't do so in C++.
However you can solve your problem in other way.
First, you need to bind some ID to some concrete object of structure BaseStation. You can provide this link in two ways - by holding BaseStation objects in associative containter, where keys are ID, or by holding array of BaseStation objects(as far as I can guess you are writing some sort of microcontroller code so std containers can be not available for you).
First approach code example:
//id is 4 char so it can be thought as int on most systems
std::map<int, BaseStation *> baseStations;
int * id = (int*)recvArray; //this hack is for showing how you can convert 4 char to int
//may be in your code (int id = combine32(recvArray[0])) is equvivalent
if(baseStations.find(*id) != baseStations.end()) //checking existance of object with such id
{
//ok, exists, do nothing
}
else
baseStations[*id] = new BaseStation(); //create new
baseStations[*id].timeUTC = combine64(recvArray[6]); //starting copying values
//other values copying
In second situation if you can't use associative containers or can't afford their libs\code because of microcontroller memory lack, you can use just arrays but it's not flexible at all and consumes more operations. Example:
//BaseConnection also holds field names id;
BaseConnection baseConnections[N];
int FindId(int id); //return index of element in baseConnections array with this id
BaseConnection * workingConnection = &baseConnections[FindId(combine32(recvArray[0]))];
workingConnection->timeUTC = combine64(recvArray[6]); //starting copying values
//other values copying