Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import operator 

2from numpy.fft.helper import fftshift, ifftshift, fftfreq 

3import scipy.fft._pocketfft.helper as _helper 

4import numpy as np 

5__all__ = ['fftshift', 'ifftshift', 'fftfreq', 'rfftfreq', 'next_fast_len'] 

6 

7 

8def rfftfreq(n, d=1.0): 

9 """DFT sample frequencies (for usage with rfft, irfft). 

10 

11 The returned float array contains the frequency bins in 

12 cycles/unit (with zero at the start) given a window length `n` and a 

13 sample spacing `d`:: 

14 

15 f = [0,1,1,2,2,...,n/2-1,n/2-1,n/2]/(d*n) if n is even 

16 f = [0,1,1,2,2,...,n/2-1,n/2-1,n/2,n/2]/(d*n) if n is odd 

17 

18 Parameters 

19 ---------- 

20 n : int 

21 Window length. 

22 d : scalar, optional 

23 Sample spacing. Default is 1. 

24 

25 Returns 

26 ------- 

27 out : ndarray 

28 The array of length `n`, containing the sample frequencies. 

29 

30 Examples 

31 -------- 

32 >>> from scipy import fftpack 

33 >>> sig = np.array([-2, 8, 6, 4, 1, 0, 3, 5], dtype=float) 

34 >>> sig_fft = fftpack.rfft(sig) 

35 >>> n = sig_fft.size 

36 >>> timestep = 0.1 

37 >>> freq = fftpack.rfftfreq(n, d=timestep) 

38 >>> freq 

39 array([ 0. , 1.25, 1.25, 2.5 , 2.5 , 3.75, 3.75, 5. ]) 

40 

41 """ 

42 n = operator.index(n) 

43 if n < 0: 

44 raise ValueError("n = %s is not valid. " 

45 "n must be a nonnegative integer." % n) 

46 

47 return (np.arange(1, n + 1, dtype=int) // 2) / float(n * d) 

48 

49 

50def next_fast_len(target): 

51 """ 

52 Find the next fast size of input data to `fft`, for zero-padding, etc. 

53 

54 SciPy's FFTPACK has efficient functions for radix {2, 3, 4, 5}, so this 

55 returns the next composite of the prime factors 2, 3, and 5 which is 

56 greater than or equal to `target`. (These are also known as 5-smooth 

57 numbers, regular numbers, or Hamming numbers.) 

58 

59 Parameters 

60 ---------- 

61 target : int 

62 Length to start searching from. Must be a positive integer. 

63 

64 Returns 

65 ------- 

66 out : int 

67 The first 5-smooth number greater than or equal to `target`. 

68 

69 Notes 

70 ----- 

71 .. versionadded:: 0.18.0 

72 

73 Examples 

74 -------- 

75 On a particular machine, an FFT of prime length takes 133 ms: 

76 

77 >>> from scipy import fftpack 

78 >>> min_len = 10007 # prime length is worst case for speed 

79 >>> a = np.random.randn(min_len) 

80 >>> b = fftpack.fft(a) 

81 

82 Zero-padding to the next 5-smooth length reduces computation time to 

83 211 us, a speedup of 630 times: 

84 

85 >>> fftpack.helper.next_fast_len(min_len) 

86 10125 

87 >>> b = fftpack.fft(a, 10125) 

88 

89 Rounding up to the next power of 2 is not optimal, taking 367 us to 

90 compute, 1.7 times as long as the 5-smooth size: 

91 

92 >>> b = fftpack.fft(a, 16384) 

93 

94 """ 

95 # Real transforms use regular sizes so this is backwards compatible 

96 return _helper.good_size(target, True) 

97 

98 

99def _good_shape(x, shape, axes): 

100 """Ensure that shape argument is valid for scipy.fftpack 

101 

102 scipy.fftpack does not support len(shape) < x.ndim when axes is not given. 

103 """ 

104 if shape and not axes: 

105 shape = _helper._iterable_of_int(shape, 'shape') 

106 if len(shape) != np.ndim(x): 

107 raise ValueError("when given, axes and shape arguments" 

108 " have to be of the same length") 

109 return shape