#include <ap_int.h>
#include <ap_fixed.h>
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <fstream>

#define DT ap_fixed<10,1>

DT filt(DT x);

std::string outstr(double value) {
    std::ostringstream s;
    s << value;
    std::string s1 = s.str();
    for (char &c : s1) if (c == '.') c = ',';
    return s1;
}

int main() {
    const int TOTAL_CYCLES = 150;
    int cycle = 0;

    std::ofstream outfile;
    outfile.open ("c:/proj/simdata.csv");
    outfile << "cycle;x;y\n";

    for (int clk = 0; clk < TOTAL_CYCLES; clk++) {
        // ---- Square wave: amplitude ±0.5, period 50 ----
        float square = (cycle < 50) ? 0.4f : -0.4f;
        cycle = (cycle + 1) % 100;

        // ---- Noise amplitude: uniform [-0.05, +0.05] (same ratio as ±100 on ±2000) ----
        // noise_ratio = 100 / 2000 = 0.05
        float noise = ((std::rand() % 20001) / 10000.0f) - 1.0f;  // [-1.0, +1.0]
        noise *= 0.05f;

        // ---- Combined input in [-1.05, +1.05], usually clipped in HLS ----
        float in = square + noise;
        DT x = in;
        DT y = filt(x);
        std::cout << "cycle=" << clk
                  << "  in=" << in
                  << "  out=" << (float)y
                  << "\n";
        outfile << outstr(clk) << ";"
        << outstr((float)x) << ";"
        << outstr((float)y) << "\n";
    }

    outfile.close();
 return 0;
}