Book Image

NumPy Essentials

By : Leo (Liang-Huan) Chin, Tanmay Dutta, Shane Holloway
Book Image

NumPy Essentials

By: Leo (Liang-Huan) Chin, Tanmay Dutta, Shane Holloway

Overview of this book

In today’s world of science and technology, it’s all about speed and flexibility. When it comes to scientific computing, NumPy tops the list. NumPy gives you both the speed and high productivity you need. This book will walk you through NumPy using clear, step-by-step examples and just the right amount of theory. We will guide you through wider applications of NumPy in scientific computing and will then focus on the fundamentals of NumPy, including array objects, functions, and matrices, each of them explained with practical examples. You will then learn about different NumPy modules while performing mathematical operations such as calculating the Fourier Transform; solving linear systems of equations, interpolation, extrapolation, regression, and curve fitting; and evaluating integrals and derivatives. We will also introduce you to using Cython with NumPy arrays and writing extension modules for NumPy code using the C API. This book will give you exposure to the vast NumPy library and help you build efficient, high-speed programs using a wide range of mathematical features.
Table of Contents (16 chapters)
NumPy Essentials
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface

Signal processing


In this section, we are going to use NumPy functions to simulate several signal functions and translate them to Fourier transforms. We will focus on using numpy.fft and its related functions. We hope after this section that you will get some sense of using a Fourier transformation in NumPy. The theory part will be covered in the next section.

The first example we are going to use is our heartbeat signal, which is a series of sine waves. The frequency is 60 beats per minutes (1 Hz), and our sampling period is 5 seconds long, with a sampling interval of 0.005 seconds. Let's create the sine wave first:

In [1]: time = np.arange(0, 5, .005) 
In [2]: x = np.sin(2 * np.pi * 1 * time) 
In [3]: y = np.fft.fft(x) 
In [4]: show(x, y) 

In this example, we first created the sampling time interval and saved it to anndarray called time. And we passed the time array times 2π and its frequency 1 Hz to the numpy.sin() method to create the sine wave (x). Then applied the...