File: recurrence_plot.c

package info (click to toggle)
lebiniou 3.67.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,500 kB
  • sloc: ansic: 28,670; makefile: 1,276; sh: 602; awk: 432; xml: 261; javascript: 23
file content (249 lines) | stat: -rw-r--r-- 5,721 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 *  Copyright 1994-2022 Olivier Girondel
 *
 *  This file is part of lebiniou.
 *
 *  lebiniou is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  lebiniou is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with lebiniou. If not, see <http://www.gnu.org/licenses/>.
 */

#include "context.h"
#include "parameters.h"
#include "pthread_utils.h"


uint32_t version = 0;
uint32_t options = BO_SFX | BO_LAST | BO_SCHEMES;
char dname[] = "Recurrence plot";
enum LayerMode mode = LM_OVERLAY;
char desc[] = "Regular/cross recurrence plot of the input";

/* parameters */
static int cross = 0;
static void set_run_ptr(void);


json_t *
get_parameters(const uint8_t fetch_all)
{
  json_t *params = json_object();

  plugin_parameters_add_boolean(params, BPP_MODE, cross, "Normal or cross-recurrence plot");

  return params;
}


void
set_parameters(const Context_t *ctx, const json_t *in_parameters)
{
  int change_ptr = 0;

  // v1 API compat
  change_ptr |= plugin_parameter_parse_int_range(in_parameters, BPP_MODE, &cross) & PLUGIN_PARAMETER_CHANGED;

  // v2 API
  change_ptr |= plugin_parameter_parse_boolean(in_parameters, BPP_MODE, &cross) & PLUGIN_PARAMETER_CHANGED;

  if (change_ptr) {
    set_run_ptr();
  }
}


json_t *
parameters(const Context_t *ctx, const json_t *in_parameters, const uint8_t fetch_all)
{
  if (NULL != in_parameters) {
    set_parameters(ctx, in_parameters);
  }

  return get_parameters(fetch_all);
}


/*
 * Les datas sont dans [-1.0..+1.0] donc la distance
 * max entre 2 points 4d dans l'espace des phases est:
 * d= sqrt (dx2+dy2+dz2+dt2) avec dx, dy, dz et dt valant au max 2 (1 - -1)
 *
 * donc max = sqrt(16) -> 4
 */
#define DMAX 4.0

/*
 * Regular recurrence plot;
 *
 * http://astronomy.swin.edu.au/~pbourke/fractals/recurrence/
 *
 * More precisely, we draw a point at coordinate (i,j) if the i'th and j'th
 * embedded vectors are less than some distance r apart, eg: a point is drawn if
 * ||yi - yj|| < r
 *
 * i is plotted along the horizontal axis, j on the vertical axis.
 *
 * we do this in 4D
 */
static inline Pixel_t
get_color(const Input_t *input, const int i, const int j)
{
  float dist;

  /* get distance between the two vectors */
  float dx = input->data[A_MONO][i+0] - input->data[A_MONO][j+0];
  float dy = input->data[A_MONO][i+1] - input->data[A_MONO][j+1];
  float dz = input->data[A_MONO][i+2] - input->data[A_MONO][j+2];
  float dt = input->data[A_MONO][i+3] - input->data[A_MONO][j+3];

  dx *= dx;
  dy *= dy;
  dz *= dz;
  dt *= dt;

  dist = sqrtf(dx + dy + dz + dt);
  dist /= DMAX;

  return (255 - (Pixel_t)(255 * dist));
}


static void
recurrence_plot(Context_t *ctx)
{
  int ii, jj;
  Buffer8_t *dst = passive_buffer(ctx);

  Buffer8_clear(dst);

  for (jj = 0; jj < MINSCREEN; jj++) {
    int j = (int)((float)jj
                  / (float)MINSCREEN
                  * (float)(ctx->input->size-3.0));
    int last_i = -1;
    Pixel_t last_c = 0;

    for (ii = jj; ii < MINSCREEN; ii++) {
      Pixel_t c = last_c;
      int i = (int)((float)ii
                    / (float)MINSCREEN
                    * (float)(ctx->input->size-3.0));

      if (i != last_i) {
        c = get_color(ctx->input, i, j);
        last_i = i;
        last_c = c;
      }

      set_pixel_nc(dst, CENTERX-HMINSCREEN+ii+1, jj, c);
      set_pixel_nc(dst, CENTERX-HMINSCREEN+jj+1, ii, c);
    }
  }
}


/*
 * Cross recurrence plot:
 *
 * http://arxiv.org/pdf/physics/0201062 (.PDF)
 * http://www.agnld.uni-potsdam.de/~marwan/rp/crps.php
 */
static inline Pixel_t
get_color_cross(const Input_t *input, const int i, const int j)
{
  float dist;

  /* get distance between the two vectors */
  float dx = input->data[A_LEFT][i+0]-input->data[A_RIGHT][j+0];
  float dy = input->data[A_LEFT][i+1]-input->data[A_RIGHT][j+1];
  float dz = input->data[A_LEFT][i+2]-input->data[A_RIGHT][j+2];
  float dt = input->data[A_LEFT][i+3]-input->data[A_RIGHT][j+3];

  dx *= dx;
  dy *= dy;
  dz *= dz;
  dt *= dt;

  dist = sqrtf(dx + dy + dz + dt);
  dist /= DMAX;

  return (255 - (Pixel_t)(255 * dist));
}


static void
cross_recurrence_plot(Context_t *ctx)
{
  int ii, jj;
  Buffer8_t *dst = passive_buffer(ctx);

  Buffer8_clear(dst);

  for (jj = 0; jj < MINSCREEN; jj++) {
    int j = (int)((float)jj
                  / (float)MINSCREEN
                  * (float)(ctx->input->size-3.0));
    int last_i = -1;
    Pixel_t last_c = 0;

    for (ii = 0; ii < MINSCREEN; ii++) {
      Pixel_t c = last_c;
      int i = (int)((float)ii
                    / (float)MINSCREEN
                    * (float)(ctx->input->size-3.0));

      if (i != last_i) {
        c = get_color_cross(ctx->input, i, j);
        last_i = i;
        last_c = c;
      }

      set_pixel_nc(dst, CENTERX-HMINSCREEN+ii+1, jj, c);
    }
  }
}


static void (*run_ptr)(struct Context_s *) = &recurrence_plot;


static void
set_run_ptr(void)
{
  if (cross) {
    run_ptr = &cross_recurrence_plot;
  } else {
    run_ptr = &recurrence_plot;
  }
}


void
on_switch_on(Context_t *ctx)
{
  int rnd_cross = b_rand_boolean();

  if (cross != rnd_cross) {
    cross = rnd_cross;
    set_run_ptr();
  }
}


void
run(Context_t *ctx)
{
  if (!xpthread_mutex_lock(&ctx->input->mutex)) {
    run_ptr(ctx);
    xpthread_mutex_unlock(&ctx->input->mutex);
  }
}