O'Reilly logo

NumPy Cookbook - Second Edition by Ivan Idris

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Broadcasting arrays

Without knowing it, you might have broadcasted arrays. In a nutshell, NumPy tries to perform an operation even though the operands do not have the same shape. In this recipe, we will multiply an array and a scalar. The scalar is extended to the shape of the array operand and then the multiplication is performed. We will download an audio file and make a new version of it that is quieter.

How to do it...

Let's start by reading a WAV file:

  1. We will use standard Python code to download an audio file of Austin Powers. SciPy has a WAV file module that allows you to load sound data or generate WAV files. If SciPy is installed, then we should already have this module. The read() function returns a data array and sample rate. In this example, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required