#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#include <sstream>
#include <vector>
#include <ap_fixed.h>

// CHECK: match with your filter  data type
#define DT ap_fixed<14,1,AP_RND,AP_SAT>

// CHECK: match with your filter function definition
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() {
    std::ofstream csv("c:/proj/testfilt2.csv");
    if (!csv.is_open()) {
        std::cerr << "Error opening CSV file\n";
        return 1;
    }

    csv << "n;x;y;freq\n";

// CHECK: test signal generator parameters
	const int num_samples = 1000;     // number of samples in CSV 
	const float fs      = 125e6;      // sampling frequency: 125 MHz
	const float f_start = 1e6;        // start frequency: 1 MHz
	const float f_end   = 50e6;       // end frequency: 50 MHz
	const float amp     = 1.0f;       // signal amplitude (14-bit signed)
	const float two_pi  = 6.283185307f;

	float phase = 0.0f;

	for (int n = 0; n < num_samples; n++) {

		float p = float(n) / float(num_samples); // sweep progress
		float f_inst = f_start + (f_end - f_start) * p; // linear sweep

		// Calculate sine signal phase
		phase += two_pi * f_inst / fs; 
		if (phase > two_pi)
			phase -= two_pi;
		
		float x_float = amp * sinf(phase); // Generate sine

		DT x = (DT)x_float;
		
// CHECK: function call 		
		DT y = filt(x);	      

		csv << n << ";" 
			<< outstr((float)x) << ";"
			<< outstr((float)y) << ";"
			<< outstr(f_inst) << "\n";
	}

    csv.close();
    std::cout << "CSV generated: fir_output.csv\n";

    return 0;
}
