<commit_before>// g++  `GraphicsMagick++-config --cxxflags --cppflags  --ldflags --libs` rigwfm.cpp -o rigwfm
// for i in *.wfm; do ./rigwfm ${i} ${i}.png; done

#include <iostream>
#include <string>
#include <list>
#include <map>
#include <vector>

#include <cstdlib>
#include <cmath>

#include <Magick++.h>

#include "rigolwfm.h"

using namespace std;
using namespace rigwfm;
using namespace Magick;


// Plot waveform as DrawablePolyline
// smoothing: n consecutive points are averaged together to do a simple low pass filter
void PlotChannel(Image & image, std::list<Magick::Drawable> & drawList, int smoothing, const RigolData & channel)
{
    double width = image.columns(), height = image.rows();
    std::list<Coordinate> vertices;
    double yscl = height/8.0/channel.scale;
    double yoff = channel.offset;
    size_t npts = channel.data.size();
    for(int j = 4; (j + smoothing) < npts; j += smoothing) {
        double y = 0;
        for(int k = 0; k < smoothing; ++k)
            y += channel.data[j + k];
        y /= smoothing;
        vertices.push_back(Coordinate((double)j/npts*width, height/2 - (y + yoff)*yscl));
    }
    drawList.push_back(DrawablePolyline(vertices));
}

void PlotWaveform(const std::string foutPath, const RigolWaveform & wfm);

int main(int argc, const char * argv[])
{
    if(argc < 3)
    {
        cerr << "Usage: rigwfm WAVEFORM_FILE IMAGE_FILE";
        return EXIT_FAILURE;
    }
    
    InitializeMagick(*argv);
    
    try {
        RigolWaveform waveform(argv[1]);
        cout << waveform << endl;
        PlotWaveform(argv[2], waveform);
    }
    catch(exception & err)
    {
        cout << "Caught exception: " << err.what() << endl;
        return EXIT_FAILURE;
    }
    
    return EXIT_SUCCESS;
}



void PlotWaveform(const std::string foutPath, const RigolWaveform & wfm)
{
    int smoothing = 4;
    
    RigolData tmpwfm;
    tmpwfm.scale = wfm.channels[0].scale;
    tmpwfm.attenuation = wfm.channels[0].attenuation;
    tmpwfm.offset = wfm.channels[0].offset;
    tmpwfm.data.resize(wfm.npoints);
    
    Image image = Image("1024x512", "white");
    
    std::list<Magick::Drawable> drawList;
    drawList.push_back(DrawablePushGraphicContext());
    drawList.push_back(DrawableViewbox(0, 0, image.columns(), image.rows()));
    
    drawList.push_back(DrawableFillColor(Color()));
    drawList.push_back(DrawableStrokeWidth(1.0));
    if(!wfm.channels[0].data.empty()) {
        drawList.push_back(DrawableStrokeColor("#F00"));
        PlotChannel(image, drawList, smoothing, wfm.channels[0]);
    }
    if(!wfm.channels[1].data.empty()) {
        drawList.push_back(DrawableStrokeColor("#00F"));
        PlotChannel(image, drawList, smoothing, wfm.channels[1]);
    }
    
    // Vertical rules
    // Trigger delay, etc are referenced from the midpoint of the captured waveform
    double midT = wfm.npoints/2.0/wfm.fs;
    double vrule;
    drawList.push_back(DrawableStrokeColor("#888"));
    for(int j = 1; j < 6; ++j) {
        vrule = wfm.fs*(midT - wfm.tscale*j)*image.columns()/wfm.npoints;
        drawList.push_back(DrawableLine(vrule, 0, vrule, image.rows()));
        vrule = wfm.fs*(midT + wfm.tscale*j)*image.columns()/wfm.npoints;
        drawList.push_back(DrawableLine(vrule, 0, vrule, image.rows()));
    }
    
    drawList.push_back(DrawableStrokeWidth(2.0));
    drawList.push_back(DrawableStrokeColor("#000"));
    vrule = wfm.fs*(midT - wfm.tscale*6)*image.columns()/wfm.npoints;
    drawList.push_back(DrawableLine(vrule, 0, vrule, image.rows()));
    vrule = wfm.fs*midT*image.columns()/wfm.npoints;
    drawList.push_back(DrawableLine(vrule, 0, vrule, image.rows()));
    vrule = wfm.fs*(midT + wfm.tscale*6)*image.columns()/wfm.npoints;
    drawList.push_back(DrawableLine(vrule, 0, vrule, image.rows()));
    
    // Trigger position
    drawList.push_back(DrawableStrokeWidth(1.0));
    drawList.push_back(DrawableStrokeColor("#990"));
    vrule = wfm.fs*(midT - wfm.tdelay)*image.columns()/wfm.npoints;
    drawList.push_back(DrawableLine(vrule, 0, vrule, image.rows()));
    
    // Horizontal rules
    double hrule;
    drawList.push_back(DrawableStrokeColor("#888"));
    for(int j = 1; j < 4; ++j) {
        hrule = image.rows() - (4 - j)*(image.rows()/8.0);
        drawList.push_back(DrawableLine(0, hrule, image.columns(), hrule));
        hrule = image.rows() - (4 + j)*(image.rows()/8.0);
        drawList.push_back(DrawableLine(0, hrule, image.columns(), hrule));
    }
    
    drawList.push_back(DrawableStrokeWidth(2.0));
    drawList.push_back(DrawableStrokeColor("#000"));
    hrule = image.rows() - (4)*(image.rows()/8.0);
    drawList.push_back(DrawableLine(0, hrule, image.columns(), hrule));
    hrule = image.rows() - (0)*(image.rows()/8.0);
    drawList.push_back(DrawableLine(0, hrule, image.columns(), hrule));
    hrule = image.rows() - (8)*(image.rows()/8.0);
    drawList.push_back(DrawableLine(0, hrule, image.columns(), hrule));
    
    // Trigger position
    // drawList.push_back(DrawableStrokeWidth(1.0));
    // drawList.push_back(DrawableStrokeColor("#990"));
    // vrule = wfm.fs*(midT - wfm.tdelay)*image.columns()/wfm.npoints;
    // drawList.push_back(DrawableLine(vrule, 0, vrule, image.rows()));
    
/*        double switchT = 2850;<commit_msg>Added 1/5th div rules to plot output, misc minor changes<commit_after>// g++  `GraphicsMagick++-config --cxxflags --cppflags  --ldflags --libs` rigwfm.cpp -o rigwfm
// for i in *.wfm; do ./rigwfm ${i} ${i}.png; done

#include <iostream>
#include <string>
#include <list>
#include <map>
#include <vector>

#include <cstdlib>
#include <cmath>

#include <Magick++.h>

#include "rigolwfm.h"

using namespace std;
using namespace rigwfm;
using namespace Magick;


// Plot waveform as DrawablePolyline
// smoothing: n consecutive points are averaged together to do a simple low pass filter
void PlotChannel(Image & image, std::list<Magick::Drawable> & drawList, int smoothing, const RigolData & channel)
{
    double width = image.columns(), height = image.rows();
    std::list<Coordinate> vertices;
    double yscl = height/8.0/channel.scale;
    double yoff = channel.offset;
    size_t npts = channel.data.size();
    for(int j = 4; (j + smoothing) < npts; j += smoothing) {
        double y = 0;
        for(int k = 0; k < smoothing; ++k)
            y += channel.data[j + k];
        y /= smoothing;
        vertices.push_back(Coordinate((double)j/npts*width, height/2 - (y + yoff)*yscl));
    }
    drawList.push_back(DrawablePolyline(vertices));
}

void PlotWaveform(const std::string foutPath, const RigolWaveform & wfm);

int main(int argc, const char * argv[])
{
    if(argc < 3)
    {
        cerr << "Usage: rigwfm WAVEFORM_FILE IMAGE_FILE";
        return EXIT_FAILURE;
    }
    
    InitializeMagick(*argv);
    
    try {
        RigolWaveform waveform(argv[1]);
        cout << waveform << endl;
        PlotWaveform(argv[2], waveform);
    }
    catch(exception & err)
    {
        cout << "Caught exception: " << err.what() << endl;
        return EXIT_FAILURE;
    }
    
    return EXIT_SUCCESS;
}



void PlotWaveform(const std::string foutPath, const RigolWaveform & wfm)
{
    int smoothing = 4;
    
    RigolData tmpwfm;
    tmpwfm.scale = wfm.channels[0].scale;
    tmpwfm.attenuation = wfm.channels[0].attenuation;
    tmpwfm.offset = wfm.channels[0].offset;
    tmpwfm.data.resize(wfm.npoints);
    
    Image image = Image("1024x512", "white");
    
    std::list<Magick::Drawable> drawList;
    drawList.push_back(DrawablePushGraphicContext());
    drawList.push_back(DrawableViewbox(0, 0, image.columns(), image.rows()));
    
    drawList.push_back(DrawableFillColor(Color()));
    
    // Vertical rules
    // Trigger delay, etc are referenced from the midpoint of the captured waveform
    double midT = wfm.npoints/2.0/wfm.fs;
    double vrule;
    drawList.push_back(DrawableStrokeColor("#444"));
    for(int j = 1; j < 6; ++j) {
        vrule = wfm.fs*(midT - wfm.tscale*j)*image.columns()/wfm.npoints;
        drawList.push_back(DrawableLine(vrule, 0, vrule, image.rows()));
        vrule = wfm.fs*(midT + wfm.tscale*j)*image.columns()/wfm.npoints;
        drawList.push_back(DrawableLine(vrule, 0, vrule, image.rows()));
    }
    drawList.push_back(DrawableStrokeWidth(0.5));
    drawList.push_back(DrawableStrokeColor("#AAA"));
    for(int j = 1; j < 7; ++j) {
        for(int k = 1; k < 5; ++k) {
            vrule = wfm.fs*(midT - wfm.tscale*(j - k/5.0))*image.columns()/wfm.npoints;
            drawList.push_back(DrawableLine(vrule, 0, vrule, image.rows()));
            vrule = wfm.fs*(midT + wfm.tscale*(j - k/5.0))*image.columns()/wfm.npoints;
            drawList.push_back(DrawableLine(vrule, 0, vrule, image.rows()));
        }
    }
    
    drawList.push_back(DrawableStrokeWidth(2.0));
    drawList.push_back(DrawableStrokeColor("#000"));
    vrule = wfm.fs*(midT - wfm.tscale*6)*image.columns()/wfm.npoints;
    drawList.push_back(DrawableLine(vrule, 0, vrule, image.rows()));
    vrule = wfm.fs*midT*image.columns()/wfm.npoints;
    drawList.push_back(DrawableLine(vrule, 0, vrule, image.rows()));
    vrule = wfm.fs*(midT + wfm.tscale*6)*image.columns()/wfm.npoints;
    drawList.push_back(DrawableLine(vrule, 0, vrule, image.rows()));
    
    // Trigger position
    drawList.push_back(DrawableStrokeWidth(1.0));
    drawList.push_back(DrawableStrokeColor("#990"));
    vrule = wfm.fs*(midT - wfm.tdelay)*image.columns()/wfm.npoints;
    drawList.push_back(DrawableLine(vrule, 0, vrule, image.rows()));
    
    // Horizontal rules
    double hrule;
    drawList.push_back(DrawableStrokeColor("#444"));
    for(int j = 1; j < 4; ++j) {
        hrule = image.rows() - (4 - j)*(image.rows()/8.0);
        drawList.push_back(DrawableLine(0, hrule, image.columns(), hrule));
        hrule = image.rows() - (4 + j)*(image.rows()/8.0);
        drawList.push_back(DrawableLine(0, hrule, image.columns(), hrule));
    }
    drawList.push_back(DrawableStrokeWidth(0.5));
    drawList.push_back(DrawableStrokeColor("#AAA"));
    for(int j = 1; j < 5; ++j) {
        for(int k = 1; k < 5; ++k) {
            hrule = image.rows() - (4 + k/5.0 - j)*(image.rows()/8.0);
            drawList.push_back(DrawableLine(0, hrule, image.columns(), hrule));
            hrule = image.rows() - (4 - k/5.0 + j)*(image.rows()/8.0);
            drawList.push_back(DrawableLine(0, hrule, image.columns(), hrule));
        }
    }
    
    drawList.push_back(DrawableStrokeWidth(2.0));
    drawList.push_back(DrawableStrokeColor("#000"));
    hrule = image.rows() - (4)*(image.rows()/8.0);
    drawList.push_back(DrawableLine(0, hrule, image.columns(), hrule));
    hrule = image.rows() - (0)*(image.rows()/8.0);
    drawList.push_back(DrawableLine(0, hrule, image.columns(), hrule));
    hrule = image.rows() - (8)*(image.rows()/8.0);
    drawList.push_back(DrawableLine(0, hrule, image.columns(), hrule));
    
    // Trigger position
    // drawList.push_back(DrawableStrokeWidth(1.0));
    // drawList.push_back(DrawableStrokeColor("#990"));
    // vrule = wfm.fs*(midT - wfm.tdelay)*image.columns()/wfm.npoints;
    // drawList.push_back(DrawableLine(vrule, 0, vrule, image.rows()));
    
    
    drawList.push_back(DrawableStrokeWidth(1.0));
    if(!wfm.channels[0].data.empty()) {
        drawList.push_back(DrawableStrokeColor("#F00"));
        PlotChannel(image, drawList, smoothing, wfm.channels[0]);
    }
    if(!wfm.channels[1].data.empty()) {
        drawList.push_back(DrawableStrokeColor("#00F"));
        PlotChannel(image, drawList, smoothing, wfm.channels[1]);
    }
    
/*        double switchT = 2850;
{'fraction_non_alphanumeric': 0.1640470669071545, 'fraction_numerical': 0.019410804775401528, 'mean_word_length': 3.240349599417334, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3157428', 'n_tokens_mistral': 4280, 'n_tokens_neox': 4162, 'n_words': 750}