MCPcopy Create free account
hub / github.com/KaisenAmin/c_std / plot_export_csv

Function plot_export_csv

plot/plot.c:1889–1952  ·  view source on GitHub ↗

* @brief Dump every series of the plot to @p filename in CSV form. * * Writes a CSV file with one row per data point and one column per * series. The first column is named @c x and is built as follows: * - if the plot's first series has an explicit @c xData array, the * value at row r is taken from `series[0].xData[r]`; * - otherwise the integer row index r is written (matching the

Source from the content-addressed store, hash-verified

1887}
1888
1889
1890/**
1891 * @brief Arithmetic mean of y values in the series at @p index.
1892 *
1893 * Computes the sum of the series in a @c double accumulator and
1894 * divides by the series length, then casts the result back to
1895 * @c float. Empty series are rejected (returning false) rather than
1896 * producing a NaN from a 0/0 division.
1897 *
1898 * @param plot Plot to query. Must be non-NULL.
1899 * @param index Zero-based series index. Must be in range.
1900 * @param out Receives the mean on success. Must be non-NULL.
1901 *
1902 * @return true on success; false if @p plot or @p out is NULL,
1903 * @p index is out of range, or the series is empty.
1904 *
1905 * @code
1906 * float avg;
1907 * if (plot_series_mean(plot, 0, &avg)) {
1908 * printf("series 0 averages %g\n", (double)avg);
1909 * }
1910 * @endcode
1911 */
1912bool plot_series_mean(const Plot* plot, size_t index, float* out) {
1913 PLOT_LOG("[plot_series_mean]: enter (plot=%p, index=%zu)", (const void*)plot, index);
1914 if (!plot || !out || index >= plot->seriesCount) {
1915 PLOT_LOG("[plot_series_mean]: bad arg or OOR -> false");
1916 return false;
1917 }
1918
1919 const PlotSeries* s = &plot->series[index];
1920 if (s->dataSize == 0) {
1921 PLOT_LOG("[plot_series_mean]: empty series -> false");
1922 return false;
1923 }
1924
1925 double acc = 0.0;
1926 for (size_t i = 0; i < s->dataSize; ++i) {
1927 acc += (double)s->yData[i];
1928 }
1929 *out = (float)(acc / (double)s->dataSize);
1930
1931 PLOT_LOG("[plot_series_mean]: exit -> true (mean=%g)", acc / (double)s->dataSize);
1932 return true;
1933}
1934
1935
1936/**
1937 * @brief Fill @p out with @p n linearly spaced values between @p start
1938 * and @p end (inclusive at both ends).
1939 *
1940 * Equivalent to NumPy's `linspace(start, end, n)`: the resulting array
1941 * has @c out[0] == start and @c out[n-1] == end (modulo float
1942 * rounding), with the remaining @p n-2 values evenly distributed
1943 * between them. Special-cased:
1944 * - @p n == 0 -> rejected, returns false.
1945 * - @p n == 1 -> only writes @c out[0] = start.
1946 * - @p n >= 2 -> uses step `(end - start) / (n - 1)`, evaluated in

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected