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

""" 

Differential and pseudo-differential operators. 

""" 

# Created by Pearu Peterson, September 2002 

from __future__ import division, print_function, absolute_import 

 

 

__all__ = ['diff', 

'tilbert','itilbert','hilbert','ihilbert', 

'cs_diff','cc_diff','sc_diff','ss_diff', 

'shift'] 

 

from numpy import pi, asarray, sin, cos, sinh, cosh, tanh, iscomplexobj 

from . import convolve 

 

from scipy.fftpack.basic import _datacopied 

 

import atexit 

atexit.register(convolve.destroy_convolve_cache) 

del atexit 

 

 

_cache = {} 

 

 

def diff(x,order=1,period=None, _cache=_cache): 

""" 

Return k-th derivative (or integral) of a periodic sequence x. 

 

If x_j and y_j are Fourier coefficients of periodic functions x 

and y, respectively, then:: 

 

y_j = pow(sqrt(-1)*j*2*pi/period, order) * x_j 

y_0 = 0 if order is not 0. 

 

Parameters 

---------- 

x : array_like 

Input array. 

order : int, optional 

The order of differentiation. Default order is 1. If order is 

negative, then integration is carried out under the assumption 

that ``x_0 == 0``. 

period : float, optional 

The assumed period of the sequence. Default is ``2*pi``. 

 

Notes 

----- 

If ``sum(x, axis=0) = 0`` then ``diff(diff(x, k), -k) == x`` (within 

numerical accuracy). 

 

For odd order and even ``len(x)``, the Nyquist mode is taken zero. 

 

""" 

tmp = asarray(x) 

if order == 0: 

return tmp 

if iscomplexobj(tmp): 

return diff(tmp.real,order,period)+1j*diff(tmp.imag,order,period) 

if period is not None: 

c = 2*pi/period 

else: 

c = 1.0 

n = len(x) 

omega = _cache.get((n,order,c)) 

if omega is None: 

if len(_cache) > 20: 

while _cache: 

_cache.popitem() 

 

def kernel(k,order=order,c=c): 

if k: 

return pow(c*k,order) 

return 0 

omega = convolve.init_convolution_kernel(n,kernel,d=order, 

zero_nyquist=1) 

_cache[(n,order,c)] = omega 

overwrite_x = _datacopied(tmp, x) 

return convolve.convolve(tmp,omega,swap_real_imag=order % 2, 

overwrite_x=overwrite_x) 

 

 

del _cache 

 

 

_cache = {} 

 

 

def tilbert(x, h, period=None, _cache=_cache): 

""" 

Return h-Tilbert transform of a periodic sequence x. 

 

If x_j and y_j are Fourier coefficients of periodic functions x 

and y, respectively, then:: 

 

y_j = sqrt(-1)*coth(j*h*2*pi/period) * x_j 

y_0 = 0 

 

Parameters 

---------- 

x : array_like 

The input array to transform. 

h : float 

Defines the parameter of the Tilbert transform. 

period : float, optional 

The assumed period of the sequence. Default period is ``2*pi``. 

 

Returns 

------- 

tilbert : ndarray 

The result of the transform. 

 

Notes 

----- 

If ``sum(x, axis=0) == 0`` and ``n = len(x)`` is odd then 

``tilbert(itilbert(x)) == x``. 

 

If ``2 * pi * h / period`` is approximately 10 or larger, then 

numerically ``tilbert == hilbert`` 

(theoretically oo-Tilbert == Hilbert). 

 

For even ``len(x)``, the Nyquist mode of ``x`` is taken zero. 

 

""" 

tmp = asarray(x) 

if iscomplexobj(tmp): 

return tilbert(tmp.real, h, period) + \ 

1j * tilbert(tmp.imag, h, period) 

 

if period is not None: 

h = h * 2 * pi / period 

 

n = len(x) 

omega = _cache.get((n, h)) 

if omega is None: 

if len(_cache) > 20: 

while _cache: 

_cache.popitem() 

 

def kernel(k, h=h): 

if k: 

return 1.0/tanh(h*k) 

 

return 0 

 

omega = convolve.init_convolution_kernel(n, kernel, d=1) 

_cache[(n,h)] = omega 

 

overwrite_x = _datacopied(tmp, x) 

return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x) 

 

 

del _cache 

 

 

_cache = {} 

 

 

def itilbert(x,h,period=None, _cache=_cache): 

""" 

Return inverse h-Tilbert transform of a periodic sequence x. 

 

If ``x_j`` and ``y_j`` are Fourier coefficients of periodic functions x 

and y, respectively, then:: 

 

y_j = -sqrt(-1)*tanh(j*h*2*pi/period) * x_j 

y_0 = 0 

 

For more details, see `tilbert`. 

 

""" 

tmp = asarray(x) 

if iscomplexobj(tmp): 

return itilbert(tmp.real,h,period) + \ 

1j*itilbert(tmp.imag,h,period) 

if period is not None: 

h = h*2*pi/period 

n = len(x) 

omega = _cache.get((n,h)) 

if omega is None: 

if len(_cache) > 20: 

while _cache: 

_cache.popitem() 

 

def kernel(k,h=h): 

if k: 

return -tanh(h*k) 

return 0 

omega = convolve.init_convolution_kernel(n,kernel,d=1) 

_cache[(n,h)] = omega 

overwrite_x = _datacopied(tmp, x) 

return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x) 

 

 

del _cache 

 

 

_cache = {} 

 

 

def hilbert(x, _cache=_cache): 

""" 

Return Hilbert transform of a periodic sequence x. 

 

If x_j and y_j are Fourier coefficients of periodic functions x 

and y, respectively, then:: 

 

y_j = sqrt(-1)*sign(j) * x_j 

y_0 = 0 

 

Parameters 

---------- 

x : array_like 

The input array, should be periodic. 

_cache : dict, optional 

Dictionary that contains the kernel used to do a convolution with. 

 

Returns 

------- 

y : ndarray 

The transformed input. 

 

See Also 

-------- 

scipy.signal.hilbert : Compute the analytic signal, using the Hilbert 

transform. 

 

Notes 

----- 

If ``sum(x, axis=0) == 0`` then ``hilbert(ihilbert(x)) == x``. 

 

For even len(x), the Nyquist mode of x is taken zero. 

 

The sign of the returned transform does not have a factor -1 that is more 

often than not found in the definition of the Hilbert transform. Note also 

that `scipy.signal.hilbert` does have an extra -1 factor compared to this 

function. 

 

""" 

tmp = asarray(x) 

if iscomplexobj(tmp): 

return hilbert(tmp.real)+1j*hilbert(tmp.imag) 

n = len(x) 

omega = _cache.get(n) 

if omega is None: 

if len(_cache) > 20: 

while _cache: 

_cache.popitem() 

 

def kernel(k): 

if k > 0: 

return 1.0 

elif k < 0: 

return -1.0 

return 0.0 

omega = convolve.init_convolution_kernel(n,kernel,d=1) 

_cache[n] = omega 

overwrite_x = _datacopied(tmp, x) 

return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x) 

 

 

del _cache 

 

 

def ihilbert(x): 

""" 

Return inverse Hilbert transform of a periodic sequence x. 

 

If ``x_j`` and ``y_j`` are Fourier coefficients of periodic functions x 

and y, respectively, then:: 

 

y_j = -sqrt(-1)*sign(j) * x_j 

y_0 = 0 

 

""" 

return -hilbert(x) 

 

 

_cache = {} 

 

 

def cs_diff(x, a, b, period=None, _cache=_cache): 

""" 

Return (a,b)-cosh/sinh pseudo-derivative of a periodic sequence. 

 

If ``x_j`` and ``y_j`` are Fourier coefficients of periodic functions x 

and y, respectively, then:: 

 

y_j = -sqrt(-1)*cosh(j*a*2*pi/period)/sinh(j*b*2*pi/period) * x_j 

y_0 = 0 

 

Parameters 

---------- 

x : array_like 

The array to take the pseudo-derivative from. 

a, b : float 

Defines the parameters of the cosh/sinh pseudo-differential 

operator. 

period : float, optional 

The period of the sequence. Default period is ``2*pi``. 

 

Returns 

------- 

cs_diff : ndarray 

Pseudo-derivative of periodic sequence `x`. 

 

Notes 

----- 

For even len(`x`), the Nyquist mode of `x` is taken as zero. 

 

""" 

tmp = asarray(x) 

if iscomplexobj(tmp): 

return cs_diff(tmp.real,a,b,period) + \ 

1j*cs_diff(tmp.imag,a,b,period) 

if period is not None: 

a = a*2*pi/period 

b = b*2*pi/period 

n = len(x) 

omega = _cache.get((n,a,b)) 

if omega is None: 

if len(_cache) > 20: 

while _cache: 

_cache.popitem() 

 

def kernel(k,a=a,b=b): 

if k: 

return -cosh(a*k)/sinh(b*k) 

return 0 

omega = convolve.init_convolution_kernel(n,kernel,d=1) 

_cache[(n,a,b)] = omega 

overwrite_x = _datacopied(tmp, x) 

return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x) 

 

 

del _cache 

 

 

_cache = {} 

 

 

def sc_diff(x, a, b, period=None, _cache=_cache): 

""" 

Return (a,b)-sinh/cosh pseudo-derivative of a periodic sequence x. 

 

If x_j and y_j are Fourier coefficients of periodic functions x 

and y, respectively, then:: 

 

y_j = sqrt(-1)*sinh(j*a*2*pi/period)/cosh(j*b*2*pi/period) * x_j 

y_0 = 0 

 

Parameters 

---------- 

x : array_like 

Input array. 

a,b : float 

Defines the parameters of the sinh/cosh pseudo-differential 

operator. 

period : float, optional 

The period of the sequence x. Default is 2*pi. 

 

Notes 

----- 

``sc_diff(cs_diff(x,a,b),b,a) == x`` 

For even ``len(x)``, the Nyquist mode of x is taken as zero. 

 

""" 

tmp = asarray(x) 

if iscomplexobj(tmp): 

return sc_diff(tmp.real,a,b,period) + \ 

1j*sc_diff(tmp.imag,a,b,period) 

if period is not None: 

a = a*2*pi/period 

b = b*2*pi/period 

n = len(x) 

omega = _cache.get((n,a,b)) 

if omega is None: 

if len(_cache) > 20: 

while _cache: 

_cache.popitem() 

 

def kernel(k,a=a,b=b): 

if k: 

return sinh(a*k)/cosh(b*k) 

return 0 

omega = convolve.init_convolution_kernel(n,kernel,d=1) 

_cache[(n,a,b)] = omega 

overwrite_x = _datacopied(tmp, x) 

return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x) 

 

 

del _cache 

 

 

_cache = {} 

 

 

def ss_diff(x, a, b, period=None, _cache=_cache): 

""" 

Return (a,b)-sinh/sinh pseudo-derivative of a periodic sequence x. 

 

If x_j and y_j are Fourier coefficients of periodic functions x 

and y, respectively, then:: 

 

y_j = sinh(j*a*2*pi/period)/sinh(j*b*2*pi/period) * x_j 

y_0 = a/b * x_0 

 

Parameters 

---------- 

x : array_like 

The array to take the pseudo-derivative from. 

a,b 

Defines the parameters of the sinh/sinh pseudo-differential 

operator. 

period : float, optional 

The period of the sequence x. Default is ``2*pi``. 

 

Notes 

----- 

``ss_diff(ss_diff(x,a,b),b,a) == x`` 

 

""" 

tmp = asarray(x) 

if iscomplexobj(tmp): 

return ss_diff(tmp.real,a,b,period) + \ 

1j*ss_diff(tmp.imag,a,b,period) 

if period is not None: 

a = a*2*pi/period 

b = b*2*pi/period 

n = len(x) 

omega = _cache.get((n,a,b)) 

if omega is None: 

if len(_cache) > 20: 

while _cache: 

_cache.popitem() 

 

def kernel(k,a=a,b=b): 

if k: 

return sinh(a*k)/sinh(b*k) 

return float(a)/b 

omega = convolve.init_convolution_kernel(n,kernel) 

_cache[(n,a,b)] = omega 

overwrite_x = _datacopied(tmp, x) 

return convolve.convolve(tmp,omega,overwrite_x=overwrite_x) 

 

 

del _cache 

 

 

_cache = {} 

 

 

def cc_diff(x, a, b, period=None, _cache=_cache): 

""" 

Return (a,b)-cosh/cosh pseudo-derivative of a periodic sequence. 

 

If x_j and y_j are Fourier coefficients of periodic functions x 

and y, respectively, then:: 

 

y_j = cosh(j*a*2*pi/period)/cosh(j*b*2*pi/period) * x_j 

 

Parameters 

---------- 

x : array_like 

The array to take the pseudo-derivative from. 

a,b : float 

Defines the parameters of the sinh/sinh pseudo-differential 

operator. 

period : float, optional 

The period of the sequence x. Default is ``2*pi``. 

 

Returns 

------- 

cc_diff : ndarray 

Pseudo-derivative of periodic sequence `x`. 

 

Notes 

----- 

``cc_diff(cc_diff(x,a,b),b,a) == x`` 

 

""" 

tmp = asarray(x) 

if iscomplexobj(tmp): 

return cc_diff(tmp.real,a,b,period) + \ 

1j*cc_diff(tmp.imag,a,b,period) 

if period is not None: 

a = a*2*pi/period 

b = b*2*pi/period 

n = len(x) 

omega = _cache.get((n,a,b)) 

if omega is None: 

if len(_cache) > 20: 

while _cache: 

_cache.popitem() 

 

def kernel(k,a=a,b=b): 

return cosh(a*k)/cosh(b*k) 

omega = convolve.init_convolution_kernel(n,kernel) 

_cache[(n,a,b)] = omega 

overwrite_x = _datacopied(tmp, x) 

return convolve.convolve(tmp,omega,overwrite_x=overwrite_x) 

 

 

del _cache 

 

 

_cache = {} 

 

 

def shift(x, a, period=None, _cache=_cache): 

""" 

Shift periodic sequence x by a: y(u) = x(u+a). 

 

If x_j and y_j are Fourier coefficients of periodic functions x 

and y, respectively, then:: 

 

y_j = exp(j*a*2*pi/period*sqrt(-1)) * x_f 

 

Parameters 

---------- 

x : array_like 

The array to take the pseudo-derivative from. 

a : float 

Defines the parameters of the sinh/sinh pseudo-differential 

period : float, optional 

The period of the sequences x and y. Default period is ``2*pi``. 

""" 

tmp = asarray(x) 

if iscomplexobj(tmp): 

return shift(tmp.real,a,period)+1j*shift(tmp.imag,a,period) 

if period is not None: 

a = a*2*pi/period 

n = len(x) 

omega = _cache.get((n,a)) 

if omega is None: 

if len(_cache) > 20: 

while _cache: 

_cache.popitem() 

 

def kernel_real(k,a=a): 

return cos(a*k) 

 

def kernel_imag(k,a=a): 

return sin(a*k) 

omega_real = convolve.init_convolution_kernel(n,kernel_real,d=0, 

zero_nyquist=0) 

omega_imag = convolve.init_convolution_kernel(n,kernel_imag,d=1, 

zero_nyquist=0) 

_cache[(n,a)] = omega_real,omega_imag 

else: 

omega_real,omega_imag = omega 

overwrite_x = _datacopied(tmp, x) 

return convolve.convolve_z(tmp,omega_real,omega_imag, 

overwrite_x=overwrite_x) 

 

 

del _cache