123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- //
- // File: DRFMRF.cpp
- //
- // MATLAB Coder version : 5.2
- // C/C++ source code generated on : 04-Mar-2023 17:44:30
- //
- // Include Files
- #include "fun7_DRFMRF.h"
- #include "fun7_DRFMRF_data.h"
- #include "fun7_DRFMRF_initialize.h"
- #include "fun7_ifft.h"
- #include "fun7_randn.h"
- #include "coder_array.h"
- #include <cmath>
- #include <iostream>
- using namespace std;
- // Function Declarations
- static double rt_hypotd_snf(double u0, double u1);
- // Function Definitions
- //
- // Arguments : double u0
- // double u1
- // Return Type : double
- //
- static double rt_hypotd_snf(double u0, double u1)
- {
- double a;
- double y;
- a = std::abs(u0);
- y = std::abs(u1);
- if (a < y) {
- a /= y;
- y *= std::sqrt(a * a + 1.0);
- } else if (a > y) {
- y /= a;
- y = a * std::sqrt(y * y + 1.0);
- } else if (!std::isnan(y)) {
- y = a * 1.4142135623730951;
- }
- return y;
- }
- //
- // Arguments : const coder::array<double, 1U> &pulse_sig
- // double f_s
- // double B_n
- // double forward_time_length
- // double forward_times
- // coder::array<double, 2U> &rxsig_noise
- // coder::array<double, 1U> &s
- // Return Type : void
- //
- void DRFMRF(const coder::array<double, 1U> &pulse_sig, double f_s, double B_n,
- double forward_time_length, double forward_times,
- coder::array<double, 2U> &rxsig_noise, coder::array<double, 1U> &s)
- {
- coder::array<creal_T, 2U> S;
- coder::array<creal_T, 2U> s_n;
- coder::array<double, 2U> b_b;
- coder::array<double, 2U> b_r;
- coder::array<double, 2U> pulse_sig_noise_t;
- coder::array<double, 2U> r;
- coder::array<double, 2U> varargin_2;
- coder::array<double, 1U> absdiff;
- coder::array<boolean_T, 1U> x;
- double b[2];
- double bsum_im;
- double bsum_re;
- double idx1;
- double xbar_im;
- int N;
- int ibcol;
- unsigned int idx_data;
- int ii_data;
- int input_sizes_idx_0;
- int k;
- int nblocks;
- int nx;
- if (!isInitialized_DRFMRF) {
- DRFMRF_initialize();
- }
- if (pulse_sig.size(0) > 100) {
- boolean_T exitg1;
- nx = pulse_sig.size(0);
- absdiff.set_size(pulse_sig.size(0));
- for (k = 0; k < nx; k++) {
- absdiff[k] = std::abs(pulse_sig[k]);
- }
- x.set_size(absdiff.size(0));
- nx = absdiff.size(0);
- for (nblocks = 0; nblocks < nx; nblocks++) {
- x[nblocks] = (absdiff[nblocks] > 0.0);
- }
- nx = 0;
- ibcol = 1;
- input_sizes_idx_0 = 0;
- exitg1 = false;
- while ((!exitg1) && (input_sizes_idx_0 <= x.size(0) - 1)) {
- if (x[input_sizes_idx_0]) {
- nx = 1;
- ii_data = input_sizes_idx_0 + 1;
- exitg1 = true;
- } else {
- input_sizes_idx_0++;
- }
- }
- if (nx == 0) {
- ibcol = 0;
- }
- for (nblocks = 0; nblocks < ibcol; nblocks++) {
- idx_data = static_cast<unsigned int>(ii_data);
- }
- N = pulse_sig.size(0);
- }
- idx1 = static_cast<double>(idx_data) + std::round(f_s * 2.0E-6);
- bsum_re = std::round(f_s * forward_time_length);
- r.set_size(1, static_cast<int>(bsum_re) + 1);
- nx = static_cast<int>(bsum_re);
- for (nblocks = 0; nblocks <= nx; nblocks++) {
- r[nblocks] = idx1 + static_cast<double>(nblocks);
- }
- b[0] = idx1 + static_cast<double>(r.size(1));
- varargin_2.set_size(r.size(1) * static_cast<int>(forward_times), 1);
- nx = r.size(1);
- ii_data = static_cast<int>(forward_times);
- for (input_sizes_idx_0 = 0; input_sizes_idx_0 < ii_data;
- input_sizes_idx_0++) {
- ibcol = input_sizes_idx_0 * nx;
- for (k = 0; k < nx; k++) {
- varargin_2[ibcol + k] = pulse_sig[static_cast<int>(r[k]) - 1];
- }
- }
- if (static_cast<int>(b[0]) != 0) {
- input_sizes_idx_0 = static_cast<int>(b[0]);
- } else {
- input_sizes_idx_0 = 0;
- }
- if (varargin_2.size(0) != 0) {
- nx = varargin_2.size(0);
- } else {
- nx = 0;
- }
- pulse_sig_noise_t.set_size(input_sizes_idx_0 + nx, 1);
- for (nblocks = 0; nblocks < input_sizes_idx_0; nblocks++) {
- pulse_sig_noise_t[nblocks] = 0.0;
- }
- for (nblocks = 0; nblocks < nx; nblocks++) {
- pulse_sig_noise_t[nblocks + input_sizes_idx_0] = varargin_2[nblocks];
- }
- if (static_cast<double>(r.size(1)) * forward_times + idx1 >=
- pulse_sig.size(0)) {
- nx = pulse_sig.size(0);
- rxsig_noise.set_size(pulse_sig.size(0), 1);
- for (nblocks = 0; nblocks < nx; nblocks++) {
- rxsig_noise[nblocks] = pulse_sig_noise_t[nblocks];
- }
- } else {
- nx = pulse_sig_noise_t.size(0);
- if (nx <= 1) {
- nx = 1;
- }
- if (pulse_sig_noise_t.size(0) == 0) {
- nx = 0;
- }
- if (pulse_sig.size(0) > nx) {
- nx = pulse_sig_noise_t.size(0);
- if (nx <= 1) {
- nx = 1;
- }
- if (pulse_sig_noise_t.size(0) == 0) {
- nx = 0;
- }
- nx = pulse_sig.size(0) - nx;
- } else {
- nx = 0;
- }
- if (pulse_sig_noise_t.size(0) != 0) {
- input_sizes_idx_0 = pulse_sig_noise_t.size(0);
- } else {
- input_sizes_idx_0 = 0;
- }
- if (nx == 0) {
- nx = 0;
- }
- rxsig_noise.set_size(input_sizes_idx_0 + nx, 1);
- for (nblocks = 0; nblocks < input_sizes_idx_0; nblocks++) {
- rxsig_noise[nblocks] = pulse_sig_noise_t[nblocks];
- }
- for (nblocks = 0; nblocks < nx; nblocks++) {
- rxsig_noise[nblocks + input_sizes_idx_0] = 0.0;
- }
- }
- idx1 = std::round(static_cast<double>(N) * (B_n / (2.0 * f_s)));
- b[0] = 1.0;
- b[1] = N;
- coder::randn_7(b, r);
- b_r.set_size(1, N);
- for (nblocks = 0; nblocks < N; nblocks++) {
- b_r[nblocks] = r[nblocks];
- }
- b[0] = 1.0;
- b[1] = N;
- coder::randn_7(b, r);
- b_b.set_size(1, N);
- for (nblocks = 0; nblocks < N; nblocks++) {
- b_b[nblocks] = r[nblocks];
- }
- S.set_size(1, b_r.size(1));
- nx = b_r.size(1);
- for (nblocks = 0; nblocks < nx; nblocks++) {
- S[nblocks].re = b_r[nblocks] + 0.0 * b_b[nblocks];
- S[nblocks].im = b_b[nblocks];
- }
- nblocks =
- static_cast<int>(((static_cast<double>(N) - idx1) - 1.0) + (1.0 - idx1));
- for (ii_data = 0; ii_data < nblocks; ii_data++) {
- nx = static_cast<int>(idx1 + static_cast<double>(ii_data)) - 1;
- S[nx].re = 0.0;
- S[nx].im = 0.0;
- }
- coder::ifft_7(S, s_n);
- ibcol = s_n.size(1);
- if (s_n.size(1) <= 1024) {
- nx = s_n.size(1);
- N = 0;
- nblocks = 1;
- } else {
- nx = 1024;
- nblocks = s_n.size(1) / 1024;
- N = s_n.size(1) - (nblocks << 10);
- if (N > 0) {
- nblocks++;
- } else {
- N = 1024;
- }
- }
- idx1 = s_n[0].re;
- xbar_im = s_n[0].im;
- for (k = 2; k <= nx; k++) {
- idx1 += s_n[k - 1].re;
- xbar_im += s_n[k - 1].im;
- }
- for (int ib{2}; ib <= nblocks; ib++) {
- nx = (ib - 1) << 10;
- bsum_re = s_n[nx].re;
- bsum_im = s_n[nx].im;
- if (ib == nblocks) {
- ii_data = N;
- } else {
- ii_data = 1024;
- }
- for (k = 2; k <= ii_data; k++) {
- input_sizes_idx_0 = (nx + k) - 1;
- bsum_re += s_n[input_sizes_idx_0].re;
- bsum_im += s_n[input_sizes_idx_0].im;
- }
- idx1 += bsum_re;
- xbar_im += bsum_im;
- }
- if (xbar_im == 0.0) {
- bsum_im = idx1 / static_cast<double>(s_n.size(1));
- idx1 = 0.0;
- } else if (idx1 == 0.0) {
- bsum_im = 0.0;
- idx1 = xbar_im / static_cast<double>(s_n.size(1));
- } else {
- bsum_im = idx1 / static_cast<double>(s_n.size(1));
- idx1 = xbar_im / static_cast<double>(s_n.size(1));
- }
- absdiff.set_size(s_n.size(1));
- for (k = 0; k < ibcol; k++) {
- absdiff[k] = rt_hypotd_snf(s_n[k].re - bsum_im, s_n[k].im - idx1);
- }
- xbar_im = 0.0;
- idx1 = 3.3121686421112381E-170;
- nx = s_n.size(1);
- for (k = 0; k < nx; k++) {
- if (absdiff[k] > idx1) {
- bsum_re = idx1 / absdiff[k];
- xbar_im = xbar_im * bsum_re * bsum_re + 1.0;
- idx1 = absdiff[k];
- } else {
- bsum_re = absdiff[k] / idx1;
- xbar_im += bsum_re * bsum_re;
- }
- }
- xbar_im = idx1 * std::sqrt(xbar_im);
- xbar_im /= std::sqrt(static_cast<double>(s_n.size(1)) - 1.0);
- s_n.set_size(1, s_n.size(1));
- nx = s_n.size(1) - 1;
- for (nblocks = 0; nblocks <= nx; nblocks++) {
- idx1 = s_n[nblocks].re;
- bsum_re = s_n[nblocks].im;
- if (bsum_re == 0.0) {
- bsum_im = idx1 / xbar_im;
- idx1 = 0.0;
- } else if (idx1 == 0.0) {
- bsum_im = 0.0;
- idx1 = bsum_re / xbar_im;
- } else {
- bsum_im = idx1 / xbar_im;
- idx1 = bsum_re / xbar_im;
- }
- s_n[nblocks].re = bsum_im;
- s_n[nblocks].im = idx1;
- }
- s.set_size(rxsig_noise.size(0));
- nx = rxsig_noise.size(0);
- for (nblocks = 0; nblocks < nx; nblocks++) {
- s[nblocks] = rxsig_noise[nblocks] * s_n[nblocks].re;
- }
- }
- static coder::array<double, 1U> argInit_Unboundedx1_real_T()
- {
- coder::array<double, 1U> result;
- // Set the size of the array.
- // Change this size to the value that the application requires.
- result.set_size(101);
- // Loop over the array to initialize each element.
- for (int idx0{0}; idx0 < result.size(0); idx0++) {
- // Set the value of the array element.
- // Change this value to the value that the application requires.
- result[idx0] = 2;
- }
- return result;
- }
- //int main()
- //{
- // coder::array<double, 1U> pulse_sig;
- // coder::array<double, 2U> rxsig_noise;
- // coder::array<double, 1U> s;
- // double f_s = 10e6;
- // double B_n = 1e6;
- // double forward_time_length = 2e-6;
- // double forward_times = 4;
- // pulse_sig = argInit_Unboundedx1_real_T();
- //
- // // Call the entry-point 'DRFMRF'.
- // DRFMRF(pulse_sig, f_s, B_n, forward_time_length, forward_times, rxsig_noise, s);
- // cout << "rxsig_noise=" <<endl;
- // cout << "rxsig_noise's length =" <<rxsig_noise.size(0)<<endl;
- // for (int i = 0; i < rxsig_noise.size(0); i++) {
- // std::cout << rxsig_noise[i] << " ";
- // }
- // cout << endl;
- // cout << "S's length =" <<s.size(0)<<endl;
- // cout << "S=" <<endl;
- // for (int i = 0; i < s.size(0); i++) {
- // std::cout << s[i] << " ";
- // }
- // return 0;
- //}
|