Dup Ver Goto 📝

LearningScipy_001

PT2/scipy/learning python scipy numpy does not exist
To
61 lines, 350 words, 2409 chars Page 'LearningScipy_001' does not exist.

I'm working through the Numpy Cookbook 2nd Edition, though slightly old, and learning scipy along the way. I'm doing some of this in Jupyter Lab, so I can post notebooks as I go along.

HTML

Notebooks

The samples come from sample packs, so I can't share those.

Misc

tomono.py

#!/usr/bin/env python
import scipy.io.wavfile as wavfile
import numpy as np
import sys
try:
  ifn,ofn = sys.argv[1:]
except ValueError:
  print(f"{sys.argv[0]} input_filename output_filename")
  exit(1)

def proc(ifn,ofn):
  sr, data = wavfile.read(ifn)
  if np.issubdtype(data.dtype,np.integer):
    dti = np.iinfo(data.dtype)
    m = -dti.min
    M = dti.max
    m = max(m,M)
    data = data.astype("float64")/m
  elif np.issubdtype(data.dtype,np.floating):
    data = data.astype("float64")
  else:
    raise Exception("Neither int nor float")

  shp = data.shape
  if len(shp) > 1:
    nchs = shp[1]
    mono = data.sum(axis=1)/nchs
  else:
    mono = data

  wavfile.write(ofn,sr,mono)
  print(f"Written {ofn}")


def main():
  proc(ifn,ofn)

if __name__ == "__main__":
  main()