Book Image

NumPy Cookbook - Second Edition

By : Ivan Idris
Book Image

NumPy Cookbook - Second Edition

By: Ivan Idris

Overview of this book

<p>NumPy has the ability to give you speed and high productivity. High performance calculations can be done easily with clean and efficient code, and it allows you to execute complex algebraic and mathematical computations in no time.</p> <p>This book will give you a solid foundation in NumPy arrays and universal functions. Starting with the installation and configuration of IPython, you'll learn about advanced indexing and array concepts along with commonly used yet effective functions. You will then cover practical concepts such as image processing, special arrays, and universal functions. You will also learn about plotting with Matplotlib and the related SciPy project with the help of examples. At the end of the book, you will study how to explore atmospheric pressure and its related techniques. By the time you finish this book, you'll be able to write clean and fast code with NumPy.</p>
Table of Contents (19 chapters)
NumPy Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Repeating audio fragments


As we saw in Chapter 2, Advanced Indexing and Array Concepts, we can do neat things with WAV files. It's just a matter of downloading the file with the urllib2 standard Python module and loading it with SciPy. Let's download a WAV file and repeat it three times. We will skip some of the steps that we've already seen in Chapter 2, Advanced Indexing and Array Concepts.

How to do it...

  1. Although NumPy has a repeat() function, in this case, it is more appropriate to use the tile() function. The repeat() function would have the effect of enlarging the array by repeating individual elements and not repeating the contents of it. The following IPython session should clarify the difference between these functions:

    In: x = array([1, 2])
    
    In: x
    Out: array([1, 2])
    
    In: repeat(x, 3)
    Out: array([1, 1, 1, 2, 2, 2])
    
    In: tile(x, 3)
    Out: array([1, 2, 1, 2, 1, 2])
    

    Now, armed with this knowledge, apply the tile() function:

    repeated = np.tile(data, 3)
  2. Plot the audio data with matplotlib...