Advertisement
Guest User

Untitled

a guest
Nov 30th, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.19 KB | None | 0 0
  1. """`stuff` from https://github.com/OverLordGoldDragon/stuff"""
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. from numpy.fft import fft, fftshift
  5. from stuff import plot, scat, cos_f
  6.  
  7.  
  8. def show_basis(k, odd=False):
  9.     if odd:
  10.         scat(cos_f([k], Ne), show=1, title=f"cos, k={k}, N={Ne}")
  11.     scat(cos_f([k], No), show=1, title=f"cos, k={k}, N={No}")
  12.  
  13. kwr = dict(show=1, dx1=1)
  14. kwc = dict(show=1, dx1=1, complex=1, c_annot=1)
  15. #%%
  16. N = 6
  17. Ne = 2 * N      # odd  symmetry len
  18. No = 2 * N - 1  # even symmetry len
  19. s = np.array([.4, -.4, -.2, .1, -.9, .2])
  20. #%%# Symmetric ###############################################################
  21. xe = np.hstack([s, s[::-1]])
  22. xo = np.hstack([s, s[::-1][1:]])
  23.  
  24. plot(xe, title="Symmetric even-len x -- [s, s[::-1]]",     **kwr)
  25. plot(xo, title="Symmetric odd-len x  -- [s, s[::-1][1:]]", **kwr)
  26. #%%
  27. xef = fft(xe)
  28. xof = fft(xo)
  29.  
  30. plot(xef, title="DFT(x_even)", **kwc)
  31. plot(xof, title="DFT(x_odd)",  **kwc)
  32.  
  33. #%%# DFT-symmetric ###########################################################
  34. xde = np.hstack([s, s[::-1][1:-1]])
  35. xdo = np.hstack([s, s[::-1][:-1] ])
  36.  
  37. plot(xde, title="DFT-symmetric even-len x -- [s, s[::-1][1:-1]]", **kwr)
  38. plot(xdo, title="DFT-symmetric odd-len x  -- [s, s[::-1][:-1]]",  **kwr)
  39. #%%
  40. xdef = fft(xde)
  41. xdof = fft(xdo)
  42.  
  43. plot(xdef, title="DFT(xd_even)", **kwc)
  44. plot(xdof, title="DFT(xd_odd)",  **kwc)
  45. #%%# Cosine basis, even vs odd ##############################################
  46. show_basis(1, odd=1)
  47. #%%# Even case for higher k #################################################
  48. show_basis(2)
  49. show_basis(3)
  50. #%%# Pretend left-most sample isn't there
  51. scat(cos_f([1], Ne), show=0, title=f"cos, k=1, N={Ne}")
  52. plt.axvline(Ne / 2, color='tab:red'); plt.show()
  53.  
  54. scat(cos_f([1], No), show=0, title=f"cos, k=1, N={No}")
  55. plt.axvline(No / 2, color='tab:red'); plt.show()
  56. #%%# n=0 case ###############################################################
  57. plot(xde,           title="DFT-symmetric even-len x", **kwr)
  58. plot(fftshift(xde), title="DFT-symmetric even-len x, centered about n=0", **kwr)
  59.  
  60. plot(xdo,           title="DFT-symmetric odd-len x", **kwr)
  61. plot(fftshift(xdo), title="DFT-symmetric odd-len x, centered about n=0", **kwr)
  62.  
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement