Square Wave

Definition

A square wave is a type of waveform characterized by its sharp, 90° corners and a pattern that alternates between two values. It is a periodic function that produces a clear musical note when played as a sound wave. The waveform is defined by its ability to switch between two distinct levels, typically +1 and -1, in a regular, repeating pattern.

Example

An example of a square wave is a sequence of numbers that alternates between 10,000 and -10,000, repeated 50 times each. This pattern, when repeated 441 times, forms a square wave that produces the musical note A. Another common representation of a square wave is a periodic function that alternates between +1 and -1.

Fourier Series Representation

A square wave can be represented as a Fourier series with specific sinusoidal components. The Fourier coefficients for a square wave are all zero except for the ( b_n ) coefficients for odd ( n ) values, which are given by:

[ b_n = \frac{4}{n\pi} ]

This means that the square wave can be decomposed into a series of sinusoidal basis functions, where only the sine components with odd harmonics contribute to the waveform.

Visualization

The square wave can be visualized as a vector in the space of functions. In this space, it has a component length of ( \frac{4}{\pi} ) in the ( \sin(2\pi t) ) direction and a component length of ( \frac{4}{3\pi} ) in the ( \sin(6\pi t) ) direction. These are the first two components in what would be an infinite list of coordinates for the square wave in this basis.

[Figure 13.25](https://livebook.manning.com/exploring-math-for-programmers-and-data-scientists/chapter-13/figure--13-25) You can think of the square wave as a vector in the space of functions with a component length of 4/π in the sin(2πt) direction and component length of 4/3π in the sin(6πt) direction. The square wave has infinitely many more components beyond these two. Figure 13.25 You can think of the square wave as a vector in the space of functions with a component length of 4/π in the sin(2πt) direction and component length of 4/3π in the sin(6πt) direction. The square wave has infinitely many more components beyond these two.

Fourier Coefficients Calculation

To calculate the Fourier coefficients of a square wave, one can use a function like fourier_coefficients(f, N). This function takes a periodic function ( f ) with period one and a number ( N ) of desired coefficients. It treats the constant function, as well as the functions ( \cos(2n\pi t) ) and ( \sin(2n\pi t) ) for ( 1 \leq n < N ), as directions in the vector space of functions and finds the components of ( f ) in those directions. The function returns the Fourier coefficient ( a_0 ), representing the constant function, a list of Fourier coefficients ( a_1, a_2, \ldots, a_N ), and a list of Fourier coefficients ( b_1, b_2, \ldots, b_N ) as a result.

FAQ (Frequently asked questions)

What does a square wave produce when played as a sound wave?

What are the typical levels a square wave alternates between?

sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest