// Automatically generated C++ file on Tue Jul 15 01:44:10 2025 // // To build with Digital Mars C++ Compiler: // // dmc -mn -WD fourier.cpp kernel32.lib #include #include extern "C" __declspec(dllexport) int (*Display)(const char *format, ...) = 0; // works like printf() extern "C" __declspec(dllexport) const double *DegreesC = 0; // pointer to current circuit temperature extern "C" __declspec(dllexport) const int *StepNumber = 0; // pointer to current step number extern "C" __declspec(dllexport) const int *NumberSteps = 0; // pointer to estimated number of steps extern "C" __declspec(dllexport) const char* const *InstanceName = 0; // pointer to address of instance name extern "C" __declspec(dllexport) const char *QUX = 0; // path to QUX.exe extern "C" __declspec(dllexport) const bool *ForKeeps = 0; // pointer to whether being evaluated non-hypothetically extern "C" __declspec(dllexport) const bool *HoldICs = 0; // pointer to whether instance initial conditions are being held extern "C" __declspec(dllexport) int (*DFFT)(struct sComplex *u, bool inv, unsigned int N, double scale) = 0; // discrete Fast Fourier function union uData { bool b; char c; unsigned char uc; short s; unsigned short us; int i; unsigned int ui; float f; double d; long long int i64; unsigned long long int ui64; char *str; unsigned char *bytes; }; // int DllMain() must exist and return 1 for a process to load the .DLL // See https://docs.microsoft.com/en-us/windows/win32/dlls/dllmain for more information. int __stdcall DllMain(void *module, unsigned int reason, void *reserved) { return 1; } void bzero(void *ptr, unsigned int count) { unsigned char *first = (unsigned char *) ptr; unsigned char *last = first + count; while(first < last) *first++ = '\0'; } // #undef pin names lest they collide with names in any header file(s) you might include. #undef in #undef out #undef clkout struct sFOURIER { // declare the structure here double lastT; // Timestamp of the previous sample (for delta time calculation) double lastIN; // Previous input value (used for trapezoidal integration) double cumsumIN; // Cumulative sum for DC component (a0) integration double cumsumCOS1; // Cumulative sum for cosine term (a1 coefficient) integration double cumsumSIN1; // Cumulative sum for sine term (b1 coefficient) integration double a0; // DC offset/0th harmonic (average value over period) double a1; // 1st harmonic cosine coefficient (Fourier series) double b1; // 1st harmonic sine coefficient (Fourier series) double Tperiod; // Base period length for the Fourier analysis double Ttarget; // Next target time when coefficients should be recalculated bool init; // Initialization flag (false until first run) }; extern "C" __declspec(dllexport) void fourier(struct sFOURIER **opaque, double t, union uData *data) { double in = data[0].d; // input double Tperiod = data[1].d; // input parameter double &out = data[2].d; // output bool &clkout = data[3].b; // output if(!*opaque) { *opaque = (struct sFOURIER *) malloc(sizeof(struct sFOURIER)); bzero(*opaque, sizeof(struct sFOURIER)); } struct sFOURIER *inst = *opaque; // Implement module evaluation code here: // Check if the instance needs initialization if (!inst->init){ // Initialize period and target time with given Tperiod inst->Tperiod = Tperiod; inst->Ttarget = Tperiod; // Mark instance as initialized inst->init = true; } // Calculate time difference since last update double dT = t - inst->lastT; clkout = false; // Perform numerical integration of input signal (in) using trapezoidal rule inst->cumsumIN += (in + inst->lastIN)*dT/2; // trap integration // Perform numerical integration for Fourier coefficients using rectangle method // (simpler but potentially less accurate than trapezoidal) inst->cumsumCOS1 += in*cos(1*2*M_PI/Tperiod*t)*dT; // rectangle integration (simplier) inst->cumsumSIN1 += in*sin(1*2*M_PI/Tperiod*t)*dT; // rectangle integration (simplier) // Check if a period is reached if (t > inst->Ttarget){ // Calculate Fourier coefficients (DC component and first harmonic) inst->a0 = 1/Tperiod*inst->cumsumIN; // DC component (average) inst->a1 = 2/Tperiod*inst->cumsumCOS1; // Cosine coefficient of 1st harmonic inst->b1 = 2/Tperiod*inst->cumsumSIN1; // Sine coefficient of 1st harmonic // Display the calculated coefficients Display("t=%f : a0=%f; a1=%f; b1=%f\n",t,inst->a0,inst->a1,inst->b1); // Reset integration accumulators for next period inst->cumsumIN = 0; inst->cumsumCOS1 = 0; inst->cumsumSIN1 = 0; // Set new target time for next period inst->Ttarget += Tperiod; clkout = true; } // Reconstruct output signal using the calculated Fourier coefficients out = inst->a0 + inst->a1*cos(1*2*M_PI/Tperiod*t) + inst->b1*sin(1*2*M_PI/Tperiod*t); // Store current time and input value for next iteration inst->lastT = t; inst->lastIN = in; } extern "C" __declspec(dllexport) double MaxExtStepSize(struct sFOURIER *inst, double t) { return inst->Tperiod/1e4; //return 1e308; // implement a good choice of max timestep size that depends on struct sFOURIER } extern "C" __declspec(dllexport) void Destroy(struct sFOURIER *inst) { free(inst); }