/
signal.py
821 lines (606 loc) · 28.6 KB
/
signal.py
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
"""Signal analysis functions for time-series data"""
#***************************************************************************************************
# Copyright 2015, 2019 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
# Under the terms of Contract DE-NA0003525 with NTESS, the U.S. Government retains certain rights
# in this software.
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0 or in the LICENSE file in the root pyGSTi directory.
#***************************************************************************************************
import numpy as _np
from scipy.fftpack import dct as _dct
from scipy.fftpack import idct as _idct
from scipy.fftpack import fft as _fft
from scipy.fftpack import ifft as _ifft
from scipy import convolve as _convolve
import warnings as _warnings
import numpy.random as _rnd
try: from astropy.stats import LombScargle as _LombScargle
except: _LombScargle = None
from scipy.stats import chi2 as _chi2
from ... import objects as _obj
def spectrum(x, times=None, null_hypothesis=None, counts=1, frequencies='auto', transform='dct',
returnfrequencies=True):
"""
Generates a power spectrum from the input time-series data. Before converting to a power
spectrum, x is rescaled as
x - > (x - counts * null_hypothesis) / sqrt(counts * null_hypothesis * (1-null_hypothesis)),
where the arithmetic is element-wise, and `null_hypothesis` is a vector in (0,1).
If `null_hypothesis` is None it is set to the mean of x. If that mean is 0 or 1 then
the power spectrum returned is (0,1,1,1,...).
Parameters
----------
x: array
The time-series data to convert into a power spectrum
times: array, optional
The times associated with the data in `x`. This is not optional for the `lsp` transform
null_hypothesis: None or array, optional
Used to normalize the data, and should be the null hypothesis that is being tested for
the probability trajectory from which `x` is drawn. If `null_hypothesis` is None it is
set to the mean of x.
counts: int, optional
The number of counts per time-step, whereby all values of `x` are within [0,counts].
In the main usages for drift detection, `x` is the clickstream for a single measurement
outcome -- so `x` contains integers between 0 and the number of measurements at a (perhaps
coarse-grained) time. `counts` is this number of measurements per time.
frequencies: 'auto' or array, optional
The frequencies to generate the power spectrum for. Only relevant for transform=`lsp`.
transform: 'dct', 'dft' or 'lsp', optional
The transform to use to generate power spectrum. 'dct' is the Type-II discrete cosine transform
with an orthogonal normalization; 'dft' is the discrete Fourier transform with a unitary
normalization; 'lsp' is the float-meaning Lomb-Scargle periodogram with an orthogonal-like
normalization.
returnfrequencies: bool, optional
Whether to return the frequencies corrsponding to the powers
Returns
-------
if returnfrequencies:
array or None
The frequencies corresponding to the power spectrum. None is returned if the frequencies
cannot be ascertained (when `times` is not specified).
array or None
The amplitudes, that are squared to obtain the powers. None is returned when the transform
does not generate amplitudes (this is the case for `lsp`)
array
The power spectrum
"""
if transform == 'dct' or transform == 'dft':
if transform == 'dct':
modes = dct(x, null_hypothesis, counts)
powers = modes**2
elif transform == 'dft':
modes = dft(x, null_hypothesis, counts)
powers = _np.abs(modes)**2
if returnfrequencies:
if isinstance(frequencies, str):
if times is None: freqs = None
else: freqs = fourier_frequencies_from_times(times)
else:
freqs = frequencies
return freqs, modes, powers
else:
return modes, powers
elif transform == 'lsp':
freqs, powers = lsp(x, times, frequencies, null_hypothesis, counts)
modes = None
if returnfrequencies:
return freqs, modes, powers
else:
return modes, powers
else:
raise ValueError("Input `transform` type invalid!")
def standardizer(x, null_hypothesis=None, counts=1):
"""
Maps the vector x over [0, counts] as
x - > (x - counts * null_hypothesis) / sqrt(counts * null_hypothesis * (1-null_hypothesis)),
where the arithmetic is element-wise, and `null_hypothesis` is a vector in (0,1).
If `null_hypothesis` is None it is set to the mean of x. If that mean is 0 or 1 then
None is returned.
"""
mean = _np.mean(x)
if null_hypothesis is None:
null_hypothesis = mean / counts
if null_hypothesis <= 0 or null_hypothesis >= 1:
return None
normalizer = _np.sqrt(counts * null_hypothesis * (1 - null_hypothesis))
standardized_x = (x - counts * null_hypothesis) / normalizer
return standardized_x
def unstandardizer(z, null_hypothesis, counts=1):
"""
Inverts the `standardizer` function.
"""
return z * _np.sqrt(counts * null_hypothesis * (1 - null_hypothesis)) + counts * null_hypothesis
def dct(x, null_hypothesis=None, counts=1):
"""
Returns the Type-II discrete cosine transform of y, with an orthogonal normalization, where
y = (x - counts * null_hypothesis) / sqrt(counts * null_hypothesis * (1-null_hypothesis)),
where the arithmetic is element-wise, and `null_hypothesis` is a vector in (0,1).
If `null_hypothesis` is None it is set to the mean of x. If that mean is 0 or 1 then
the vector of all ones, except for the first element which is set to zero, is returned.
Parameters
----------
x : array
Data string, on which the normalization and discrete cosine transformation is performed. If
counts is not specified, this must be a bit string.
null_hypothesis : array, optional
If not None, an array to use in the normalization before the dct. If None, it is
taken to be an array in which every element is the mean of x.
counts : int, optional
A factor in the normalization, that should correspond to the counts-per-timestep (so
for full time resolution this is 1).
Returns
-------
array
The DCT modes described above.
"""
standardized_x = standardizer(x, null_hypothesis, counts)
if standardized_x is None:
out = _np.ones(len(x))
out[0] = 0.
return out
modes = _dct(standardized_x, norm='ortho')
return modes
def idct(modes, null_hypothesis, counts=1):
"""
Inverts the dct function.
Parameters
----------
modes : array
The fourier modes to be transformed to the time domain.
null_hypothesis : array
The array that was used in the normalization before the dct. This is
commonly the mean of the time-domain data vector. All elements of this
array must be in (0,1).
counts : int, optional
A factor in the normalization, that should correspond to the counts-per-timestep (so
for full time resolution this is 1).
Returns
-------
array
Inverse of the dct function
"""
z = _idct(modes, norm='ortho')
x = unstandardizer(z, null_hypothesis, counts)
return x
def dft(x, null_hypothesis=None, counts=1):
"""
Returns the discrete Fourier transform of y, with a unitary normalization, where
y is an array with elements related to the x array by
y = (x - counts * null_hypothesis) / sqrt(counts * null_hypothesis * (1-null_hypothesis)),
where the arithmetic is element-wise, and `null_hypothesis` is a vector in (0,1).
If `null_hypothesis` is None it is set to the mean of x. If that mean is 0 or 1 then
the vector of all ones, except for the first element which is set to zero, is returned.
Parameters
----------
x : array
Data string, on which the normalization and discrete cosine transformation is performed. If
counts is not specified, this must be a bit string.
null_hypothesis : array, optional
If not None, an array to use in the normalization before the dct. If None, it is
taken to be an array in which every element is the mean of x.
counts : int, optional
A factor in the normalization, that should correspond to the counts-per-timestep (so
for full time resolution this is 1).
Returns
-------
array
The DFT modes described above.
"""
standardized_x = standardizer(x, null_hypothesis, counts)
if standardized_x is None:
out = _np.ones(len(x))
out[0] = 0.
return out
modes = _fft(standardized_x) / _np.sqrt(len(x))
return modes
def idft(modes, null_hypothesis, counts=1):
"""
Inverts the dft function.
Parameters
----------
modes : array
The fourier modes to be transformed to the time domain.
null_hypothesis : array
The array that was used in the normalization before the dct. This is
commonly the mean of the time-domain data vector. All elements of this
array must be in (0,1).
counts : int, optional
A factor in the normalization, that should correspond to the counts-per-timestep (so
for full time resolution this is 1).
Returns
-------
array
Inverse of the dft function
"""
z = _np.sqrt(len(modes)) * _ifft(modes) # TIM CHECK THIS: len(*modes*) correct?
x = unstandardizer(z, null_hypothesis, counts)
return x
def lsp(x, times, frequencies='auto', null_hypothesis=None, counts=1):
"""
Performs a Lomb-Scargle periodogram (lsp) on the input data, returning powers
and frequencies.
*** This function uses astropy, which is not a required dependency for pyGSTi ***
Parameters
----------
todo
Returns
-------
todo
"""
numtimes = len(x)
if isinstance(frequencies, str):
freq = frequencies_from_timestep((max(times) - min(times)) / numtimes, numtimes)
else:
freq = frequencies
standardized_x = standardizer(x, null_hypothesis, counts)
if standardized_x is None:
out = _np.ones(len(freq))
out[0] = 0.
return out
if freq[0] == 0.:
lspfreq = freq[1:]
else:
lspfreq = freq
if _LombScargle is None:
power = [0] # TIM CHECK THIS??
else:
power = _LombScargle(times, standardized_x, fit_mean=True, center_data=False).power(lspfreq,
normalization='psd')
if freq[0] == 0.: power = _np.array([0, ] + list(power))
return freq, power
def bartlett_spectrum(x, numspectra, counts=1, null_hypothesis=None, transform='dct'):
"""
Calculates the Bartlett power spectrum. This involves splitting the data into disjoint
sections of the same length, and generating a power spectrum for each such section,
and then averaging all these power spectra.
Parameters
----------
x : array
The data to calculate the spectrum for.
numspectra : int
The number of "chunks" to split the data into, with a spectra calculated for each
chunk. Note that if len(`x`) / num_spectra is not an integer, then not all of the
data will be used.
counts : int, optional
The number of "clicks" per time-step in x, used for standarizing the data.
null_hypothesis : array, optional
The null hypothesis that we're looking for violations of. If left as None then this
is the no-drift null hypothesis, with the static probability set to the mean of the
data.
transform : str, optional
The transform to use the generate the power spectra.
Returns
-------
array
The Bartlett power spectrum
"""
length = int(_np.floor(len(x) / numspectra))
if null_hypothesis is None: null_hypothesis = _np.mean(x) * _np.ones(len(x)) / counts
spectra = _np.zeros((numspectra, length))
bartlett_spectrum = _np.zeros(length)
for i in range(0, numspectra):
junk, powers = spectrum(x[i * length:((i + 1) * length)], counts=counts,
null_hypothesis=null_hypothesis[i * length:((i + 1) * length)],
returnfrequencies=False)
spectra[i, :] = powers
bartlett_spectrum = _np.mean(spectra, axis=0)
return bartlett_spectrum
def dct_basisfunction(omega, times, starttime, timedif):
"""
The `omega`th DCT basis function, for a initial time of `starttime` and a time-step of `timedif`,
evaluated at the times `times`.
"""
return _np.array([_np.cos(omega * _np.pi * (t - starttime + 0.5) / timedif) for t in times])
def power_significance_threshold(significance, numtests, dof):
"""
The multi-test adjusted `signficance` statistical significance threshold for
testing `numtests` test statistics that all have a marginal distribution that
is chi2 with `dof` degrees of freedom.
"""
threshold = _chi2.isf(significance / numtests, dof) / dof
return threshold
def power_to_pvalue(power, dof):
"""
Converts a power to a p-value, under the assumption that the power is chi2
distribution with `dof` degrees of freedom.
"""
pvalue = 1 - _chi2.cdf(dof * power, dof)
return pvalue
def maxpower_pvalue(maxpower, numpowers, dof):
"""
The p-value of the test statistic max(lambda_i) where there are `numpowers`
lambda_i test statistics, and they are i.i.d. as chi2 with `dof` degrees
of freedom. This approximates the p-value of the largest power in "clickstream"
power spectrum (generated from `spectrum`), with the DOF given by the number of
clicks per times.
"""
pvalue = 1 - _chi2.cdf(maxpower * dof, dof) ** (numpowers - 1)
return pvalue
def power_significance_quasithreshold(significance, numstats, dof, procedure='Benjamini-Hochberg'):
"""
The Benjamini-Hockberg quasi-threshold for finding the statistically significant powers in
a power spectrum.
"""
if procedure == 'Benjamini-Hochberg':
quasithreshold = _np.array([_chi2.isf((numstats - i) * significance / numstats, dof) / dof
for i in range(numstats)])
else:
raise ValueError("Can only obtain a quasithreshold for the Benjamini-Hochberg procedure!")
return quasithreshold
def compute_auto_frequencies(ds, transform='dct'):
"""
Returns a set of frequencies to create spectra for, for the input data. These frequencies are
in units of 1 / unit where `unit` is the units of the time-stamps in the DataSet.
What this function is doing has a fundmentally different meaning depending on whether the
transform is time-stamp aware (here, the LSP) or not (here, the DCT and DFT).
Time-stamp aware transforms take the frequencies to calculate powers at *as an input*, so this
chooses these frequencies, which are, explicitly, the frequencies associated with the powers. The task
of choosing the frequencies amounts to picking the best set of frequencies at which to interogate
the true probability trajectory for components. As there are complex factors involved in this
choice that the code has no way of knowing, sometimes it is best to choose them yourself. E.g.,
if different frequencies are used for different circuits it isn't possible to (meaningfully)
averaging power spectra across circuits, but this might be preferable if the time-step is
sufficiently different between different circuits -- it depends on your aims.
For time-stamp unaware transforms, these are the frequencies that, given that we're implementing
the, e.g., DCT, the generated power spectrum is *implicitly* with respect to. In the case of data
on a fixed time-grid, i.e., equally spaced data, then there is a precise set of frequencies implicit
in the transform. Otherwise, these frequencies are explicitly at least slightly ad hoc, and choosing
these frequencies amounts to choosing those frequencies that "best" approximate the properties being
interogatted with fitting each, e.g., DCT basis function to the (timestamp-free) data.
Parameters
----------
ds: DataSet or MultiDataset
Contains time-series data that the "auto" frequencies are calculated for.
transform: str, optional
The type of transform that the frequencies are being chosen for.
Returns
-------
frequencies : list
A list of lists, where each of the consituent lists is a set of frequencies that are the
are the derived frequencies for one or more of the dataset rows (i.e., for one or circuits).
It is common for this to be a length-1 list, containing a single set of frequencies that are
the frequencies this function has chosen for every circuit.
pointers : dict
A dictionary that "points" from the index of a circuit (indexed in the order of ds.keys())
to the index of the list of frequencies for that circuit. No entry in the dictionary for
an index should be interpretted as 0. So this can be an empty dictionary (with the frequencies
list then necessarily of length 1, containing a single set of frequencies for every circuit).
"""
# future: make this function for the lsp.
assert(transform in ('dct', 'dft', 'lsp')), "The type of transform is invalid!"
# todo : This is only reasonable with data that is equally spaced per circuit and with the same
# time-step over circuits
if isinstance(ds, _obj.MultiDataSet):
dskey = list(ds.keys())[0]
timestep = ds[dskey].meantimestep
rw = ds[dskey][list(ds[dskey].keys())[0]]
elif isinstance(ds, _obj.DataSet):
timestep = ds.meantimestep
rw = ds[list(ds.keys())[0]]
else:
raise ValueError("Input data must be a DataSet or MultiDataSet!")
numtimes = rw.number_of_times
freqs = frequencies_from_timestep(timestep, numtimes)
freqslist = [freqs, ]
# This pointers list should have keys based that are indices, as required by the stabilityanalyzer object.
freqpointers = {}
return freqslist, freqpointers
def frequencies_from_timestep(timestep, numtimes):
"""
Calculates the Fourier frequencies associated with a timestep and a total number of times. These frequencies
are in 1/units, where `units` is the units of time in `times`.
Parameters
----------
timestep : float
The time difference between each data point.
numtimes : int
The total number of times.
Returns
-------
array
The frequencies associated with Fourier analysis on data with these timestamps.
"""
return _np.arange(0, numtimes) / (2 * timestep * numtimes)
# Currently this is a trivial wrap-around for `frequencies_from_timestep, but in the future it might
# do something more subtle.
def fourier_frequencies_from_times(times):
"""
Calculates the Fourier frequencies from a set of times. These frequencies are in 1/units, where
`units` is the units of time in `times`. Note that if the times are not exactly equally spaced,
then the Fourier frequencies are ill-defined, and this returns the frequencies based on assuming
that the time-step is the mean time-step. This is reasonable for small deviations from equally
spaced times, but not otherwise.
Parameters
----------
times : list
The times from which to calculate the frequencies
Returns
-------
array
The frequencies associated with Fourier analysis on data with these timestamps.
"""
timestep = _np.mean(_np.diff(times)) # The average time step.
numtimes = len(times) # The number of times steps
return frequencies_from_timestep(timestep, numtimes)
def amplitudes_at_frequencies(freq_indices, timeseries, times=None, transform='dct'):
"""
Finds the amplitudes in the data at the specified frequency indices.
Todo: better docstring. Currently only works for the DCT.
"""
amplitudes = {}
for o in timeseries.keys():
if transform == 'dct':
temp = _dct(timeseries[o], norm='ortho')[freq_indices] / _np.sqrt(len(timeseries[o]) / 2)
if 0. in freq_indices:
temp[0] = temp[0] / _np.sqrt(2)
amplitudes[o] = list(temp)
else:
raise NotImplementedError("This function only currently works for the DCT!")
return amplitudes
def sparsity(p):
"""
Returns the Hoyer sparsity index of the input vector p. This is defined to be:
HoyerIndex = (sqrt(l) - (|p|_1 / |p|_2)) / (sqrt(l) - 1)
where l is the length of the vector and |.|_1 and |.|_2 are the 1-norm and 2-norm of the vector, resp.
"""
n = len(p)
return (_np.sqrt(n) - _np.linalg.norm(p, 1) / _np.linalg.norm(p, 2)) / (_np.sqrt(n) - 1)
def renormalizer(p, method='logistic'):
"""
Takes an arbitrary input vector `p` and maps it to a vector bounded within [0,1].
- If `method` = "sharp", then it maps any value in `p` below zero to zero and any value in `p` above one to one.
- If `method` = "logistic" then it 'squashes' the vector around it's mean value using a logistic function. The
exact transform for each element x of p is:
x -> mean - nu + (2*nu) / (1 + exp(-2 * (x - mean) / nu))
where mean is the mean value of p, and nu is min(1-mean,mean). This transformation is only sensible when the
mean of p is within [0,1].
Parameters
----------
p : array of floats
The vector with values to be mapped to [0,1]
method : {'logistic', 'sharp'}
The method for "squashing" the input vector.
Returns
-------
numpy.array
The input vector "squashed" using the specified method.
"""
if method == 'logistic':
mean = _np.mean(p)
out = logistic_transform(p, mean)
elif method == 'sharp':
out = p.copy()
out[p > 1] = 1.
out[p < 0] = 0.
else: raise ValueError("method should be 'logistic' or 'sharp'")
return out
def logistic_transform(x, mean):
"""
Transforms the input float `x` as:
x -> mean - nu + (2*nu) / (1 + exp(-2 * (x - mean) / nu))
where nu is min(1-mean,mean). This is a form of logistic transform, and maps x to a value in [0,1].
"""
nu = min([1 - mean, mean])
out = mean - nu + (2 * nu) / (1 + _np.exp(-2 * (x - mean) / nu))
return out
def lowpass_filter(data, max_freq=None):
"""
Implements a low-pass filter on the input array, by DCTing the input, mapping all but the lowest
`max_freq` modes to zero, and then inverting the transform.
Parameters
----------
data : numpy.array,
The vector to low-pass filter
max_freq : None or int, optional
The highest frequency to keep. If None then it keeps the minimum of 50 or l/10 frequencies, where l is the
length of the data vector
Returns
-------
numpy.array
The low-pass-filtered data.
"""
n = len(data)
if max_freq is None:
max_freq = min(int(_np.ceil(n / 10)), 50)
modes = _dct(data, norm='ortho')
if max_freq < n - 1:
modes[max_freq + 1:] = _np.zeros(len(data) - max_freq - 1)
out = _idct(modes, norm='ortho')
return out
def moving_average(sequence, width=100):
"""
Implements a moving average on `sequence` with an averaging width of `width`.
"""
seq_length = len(sequence)
base = _convolve(_np.ones(seq_length), _np.ones((int(width),)) / float(width), mode='same')
signal = _convolve(sequence, _np.ones((int(width),)) / float(width), mode='same')
return signal / base
def generate_flat_signal(power, nummodes, n, candidatefreqs=None, base=0.5, method='sharp'):
"""
Generates a minimal sparsity probability trajectory for the specified power, and a specified number of modes
containing non-zero power. This is a probability trajectory that has a signal power of `power` (where, for a
probability trajectory p, the signal vector is s = p - mean(p)), and has that power equally spread over `nummodes`
different frequencies. The phases on the amplitudes are randomized.
Parameters
----------
power : float
The amount of power in the signal of the generated probability trajectory
nummodes : int
The number of modes to equally spread that power over.
n : int
The number of sample times that the probability trajectory is being created for.
candidatefreqs : list, optional
A list containing a subset of 1,2,...,n-1. If not None, then all frequencies are included.
base : float in (0,1), optional
The mean of the generated probability trajectory.
method : str, optional
The method used to guarantee that the created probability trajectory is a valid probability,
i.e., is within [0,1]. Options:
- None. In this case nothing is done to guarantee that the output is a valid probability
trajectory, but the power in the signal is guaranteed to be the input power.
- 'sharp' or 'logistic'. The 'sharp' or 'logistic' methods with the `renormalizer()`
function are used to map the generated vector onto [0,1]. When the renormalizer
does a non-trivial action on the generated vector, the power in the output will
be *below* the requested power. Moreover, the power will not be distributed only
over the requested number of modes (i.e., it impacts the spectrum of the output
vector).
Returns
-------
array
A probability trajectory.
"""
amppermode = _np.sqrt(power / nummodes)
if candidatefreqs is None: candidatefreqs = _np.arange(1, n)
freqs = _np.random.choice(candidatefreqs, size=nummodes, replace=False, p=None)
modes = _np.zeros(n, float)
random_phases = _np.random.binomial(1, 0.5, size=nummodes)
for i in range(0, nummodes):
modes[freqs[i]] = amppermode * (-1)**random_phases[i]
p = idct(modes, base * _np.ones(n))
if method is not None:
p = renormalizer(p, method=method)
return p
def generate_gaussian_signal(power, center, spread, n, base=0.5, method='sharp'):
"""
Generates a probability trajectory with the specified power that is approximately Gaussian distribution
accross frequencies, centered in the frequency specified by `center`. The probability trajectory has a
rescaled signal power of `power`, where, for a probability trajectory p, the signal vector is s = p - mean(p),
and the rescaled signal vector is p - mean(p) / sqrt(mean(p) * (1 - mean(p)). The phases on the amplitudes
are randomized.
Parameters
----------
power : float
The amount of power in the rescaled signal of the generated probability trajectory.
center : int
The mode on which to center the Gaussian.
spread : int
The spread of the Gaussian
n : int
The number of sample times that the probability trajectory is being created for.
base : float in (0,1), optional
The mean of the generated probability trajectory.
method : str, optional
The method used to guarantee that the created probability trajectory is a valid probability,
i.e., is within [0,1]. Options:
- None. In this case nothing is done to guarantee that the output is a valid probability
trajectory, but the power in the signal is guaranteed to be the input power.
- 'sharp' or 'logistic'. The 'sharp' or 'logistic' methods with the `renormalizer()`
function are used to map the generated vector onto [0,1]. When the renormalizer
does a non-trivial action on the generated vector, the power in the output will
be *below* the requested power. Moreover, the power will not be distributed only
over the requested number of modes (i.e., it impacts the spectrum of the output
vector).
Returns
-------
array
A probability trajectory.
"""
modes = _np.zeros(n)
modes[0] = 0.
modes[1:] = _np.exp(-1 * (_np.arange(1, n) - center)**2 / (2 * spread**2))
modes = modes * (-1)**_np.random.binomial(1, 0.5, size=n)
modes = _np.sqrt(power) * modes / _np.sqrt(sum(modes**2))
p = idct(modes, base * _np.ones(n))
if method is not None:
p = renormalizer(p, method=method)
return p