# Everyday DSP for Programmers: Transforms

Programmers work with signals all the time. We may not normally think of the data we're working with as signals, but treating it that way and learning some basic ways of processing those signals can come in quite handy. Last week I went over the basic signal types that are commonly used in digital signal processing. Today, I'll cover the basic ways we can change a signal using a set of transforms.

These transforms can be used in a variety of ways to accomplish different goals when processing signals, and we'll look at a few use cases as we go through the transforms. We'll need an example signal to apply these transforms to, and I can think of no better signal to use than the workhorse of DSP—the sine wave. The basic equation for a sine wave is

Where

Visually, an offset will shift the signal up or down on a graph without distorting it in any other way:

This graph shows the basic sine wave and equation in grey, and the transformed version in blue. Click on the graph a few times to see what happens when the offset is changed to different values. The sine wave shifts up for positive offsets and down for negative offsets. You may also notice that the equation for the sine wave isn't entirely accurate to the graph shown. We'll fix that as the other transforms are introduced.

The offset's main purpose is to either remove or add a DC component to a signal. If you recall from last week, the DC signal is a basic signal with a constant value, and if you subtract out the DC component of a signal, you're left with only the varying part of the signal. This can be useful if you are only concerned with how the signal is changing over time or what its relative value is to what it was at an earlier time, and you are not worried about what the absolute value of the signal is. Removing the DC component is also used as part of normalizing a signal, which means the extents of the signal are set to specific values—normally -1 and 1—and the rest of the signal is scaled accordingly.

Sometimes instead of removing the DC component, we want to add in a DC component. If we were normalizing to a different range, say between 0 and 1, then we would add an offset to the signal. Also, if you needed to shift a signal to compare it to a second signal, you could add an offset to it.

The

Then the offset term would also be multiplied by the amplitude, causing the signal to shift up or down depending on the value of

Here is what the amplitude transform looks like visually:

Since the base signal has an amplitude of 2, that's reflected in its equation. Notice how the signal shrinks and expands relative to the base signal as you click through the different amplitude settings, but it stays centered around the x-axis. Negative amplitudes will invert the signal in addition to scaling it, and a zero amplitude removes the signal completely. If there was an offset to the signal, all that would be left in this case would be the DC component.

In addition to being part of the normalization procedure for signals, the amplitude transform is used to scale a signal to be in the same range as other signals that may be processed together. If a signal is too large or too small, certain DSP operations won't work well, so signals are routinely scaled to be in the right range for other operations.

Since we are working with the sine function,

Since the frequency is applied to the time dimension before the sine function, it has the inverse effect of the amplitude. A larger frequency has the effect of squishing the signal together instead of expanding it like a larger amplitude would, and a smaller frequency expands the signal in the time dimension instead of shrinking it. You can see the effect of different frequency values on the basic signal in the following graph:

The frequency of zero has the effect of stretching the sine wave out flat along the x-axis, similar to the amplitude of zero, except that an amplitude of zero shrinks the signal flat instead. Likewise, a negative frequency appears to invert the sine wave, but it only looks that way because of the symmetry of the sine wave. Negative frequencies actually reflect the signal across the y-axis, so what you see is the reflection of the sine wave from the left of the y-axis.

The term 'frequency transform' only really applies if the signal in question is periodic. For non-periodic signals, it's more accurate to describe the transform as a scaling in time. It's difficult to come up with an example for the frequency transform, since it's fairly uncommon to scale the frequency of data. The most obvious example comes from the world of analog electrical engineering, namely FM radio, which directly changes the frequency of the radio signal to convey information through the air.

It may not be common to change the frequency of a signal, but

In this case,

Now

There are a number of things to notice in this graph while clicking through it. First, a positive phase will shift the signal to the left,

Coming back to the different phases in the graph, the phase of 25% reproduces the cosine wave because that's exactly what the cosine is. It's the sine wave with a 90 degree (or 25%) phase shift. Next, the phase of 50% looks the same as the negative amplitude transform because of the symmetry of the sine wave, and the phase of 100% brings us back to the original sine wave because we've now shifted in time by one full period of the sine wave. Finally, the phase of -50% looks the same as 50% because they also differ by one full period, but the signals were shifted in opposite directions. Hopefully, this gives you a good feeling on how changes in phase affect a signal.

Then we add more sine functions to it with odd multiples of the original frequency. We also need to scale down the amplitude of each successive sine wave by the same multiple of the frequency. The first three terms would be:

This is only an approximation of a square wave. To get the real thing, we'd have to continue the summation forever:

That's a lot of terms. Here's what the first 20 terms look like as they are added together (just keep clicking):

As more and more terms are added to the summation, the signal looks more and more like a square wave. The steps get more vertical, and the other regions get flatter. You can see how it would take an awful lot of terms to make a true square wave, and that is why a square wave has infinite frequency content.

That about wraps it up. The complete sine function includes the four basic transforms of offset, amplitude, frequency, and phase, and a sequence of these sine functions can be added together to represent any other signal that exists. There are other transforms, but these are the fundamental ones. We'll get into some of the other ones in later posts. For next time we'll take a look at sampling theory so we can start doing some

Basic Signals

Transforms

Sampling Theory

Averaging

Step Response of Averaging

Signal Variance

Edge Detection

Signal Envelopes

Frequency Measurement

The Discrete Fourier Transform

The DFT in Use

Spectral Peak Detection

FIR Filters

Frequency Detection

DC and Impulsive Noise Removal

These transforms can be used in a variety of ways to accomplish different goals when processing signals, and we'll look at a few use cases as we go through the transforms. We'll need an example signal to apply these transforms to, and I can think of no better signal to use than the workhorse of DSP—the sine wave. The basic equation for a sine wave is

*f(t) = sin(t)*Where

*t*is the variable of time, marching forever forward. This basic equation will get embellished with other parameters as we explore the different transforms. We'll start off with the simplest of the transforms.#### Offset

The offset transform is used to increase or decrease all of the values of a signal by the same amount. Functionally, it adds a constant value*C*to the basic equation:*f(t) = sin(t) + C*Visually, an offset will shift the signal up or down on a graph without distorting it in any other way:

This graph shows the basic sine wave and equation in grey, and the transformed version in blue. Click on the graph a few times to see what happens when the offset is changed to different values. The sine wave shifts up for positive offsets and down for negative offsets. You may also notice that the equation for the sine wave isn't entirely accurate to the graph shown. We'll fix that as the other transforms are introduced.

The offset's main purpose is to either remove or add a DC component to a signal. If you recall from last week, the DC signal is a basic signal with a constant value, and if you subtract out the DC component of a signal, you're left with only the varying part of the signal. This can be useful if you are only concerned with how the signal is changing over time or what its relative value is to what it was at an earlier time, and you are not worried about what the absolute value of the signal is. Removing the DC component is also used as part of normalizing a signal, which means the extents of the signal are set to specific values—normally -1 and 1—and the rest of the signal is scaled accordingly.

Sometimes instead of removing the DC component, we want to add in a DC component. If we were normalizing to a different range, say between 0 and 1, then we would add an offset to the signal. Also, if you needed to shift a signal to compare it to a second signal, you could add an offset to it.

#### Amplitude

The amplitude transform, also referred to as scaling the signal or changing the signal's gain, changes each value of the signal proportionally to its current value. We adjust the basic equation by adding a multiplication term:*f(t) = A sin(t) + C*The

*A*term is the amplitude of the sine wave, and notice how it is applied before the offset because of the order of operations. If we instead applied the amplitude term after the offset, like so:*f(t) = A (sin(t) + C)*Then the offset term would also be multiplied by the amplitude, causing the signal to shift up or down depending on the value of

*A*. The way the amplitude transform is applied is important, and often the DC component of the signal should be removed before scaling the signal and then added back in afterward if it is desired to keep the signal's offset constant through the transformation.Here is what the amplitude transform looks like visually:

Since the base signal has an amplitude of 2, that's reflected in its equation. Notice how the signal shrinks and expands relative to the base signal as you click through the different amplitude settings, but it stays centered around the x-axis. Negative amplitudes will invert the signal in addition to scaling it, and a zero amplitude removes the signal completely. If there was an offset to the signal, all that would be left in this case would be the DC component.

In addition to being part of the normalization procedure for signals, the amplitude transform is used to scale a signal to be in the same range as other signals that may be processed together. If a signal is too large or too small, certain DSP operations won't work well, so signals are routinely scaled to be in the right range for other operations.

#### Frequency

The frequency transform is similar to the amplitude transform, except that instead of scaling the signal's values, it scales the time dimension. The frequency is a multiplication operation applied to the*t*variable in the basic equation:*f(t) = A sin(ωt) + C*Since we are working with the sine function,

*ω*is normally specified in radians per second, and the unit value of the frequency is 2π rad/sec. That means another way to write the basic equation with the frequency in Hz (1/sec) is*f(t) = A sin(2πf·t) + C*Since the frequency is applied to the time dimension before the sine function, it has the inverse effect of the amplitude. A larger frequency has the effect of squishing the signal together instead of expanding it like a larger amplitude would, and a smaller frequency expands the signal in the time dimension instead of shrinking it. You can see the effect of different frequency values on the basic signal in the following graph:

The frequency of zero has the effect of stretching the sine wave out flat along the x-axis, similar to the amplitude of zero, except that an amplitude of zero shrinks the signal flat instead. Likewise, a negative frequency appears to invert the sine wave, but it only looks that way because of the symmetry of the sine wave. Negative frequencies actually reflect the signal across the y-axis, so what you see is the reflection of the sine wave from the left of the y-axis.

The term 'frequency transform' only really applies if the signal in question is periodic. For non-periodic signals, it's more accurate to describe the transform as a scaling in time. It's difficult to come up with an example for the frequency transform, since it's fairly uncommon to scale the frequency of data. The most obvious example comes from the world of analog electrical engineering, namely FM radio, which directly changes the frequency of the radio signal to convey information through the air.

It may not be common to change the frequency of a signal, but

*determining*the frequency content of a signal is extremely common and useful. The main tool for doing this calculation is the Fourier Transform, and it converts a signal from the time domain to the frequency domain. This is a lossless, reversible transformation, and is, in fact, an equivalent representation of the same signal. For digital signals, it is referred to as a DFT (Discrete Fourier Transform), and there's a special version of it when the number of samples used in the conversion is a power of two that can be performed much faster than a general DFT. This fast version is creatively called the FFT (Fast Fourier Transform). We'll get into the FFT much later in this series because there are a number of other simpler DSP techniques to cover first.#### Phase

The phase transform is similar to the offset transform, except that it shifts the signal back or forward in time instead of shifting it up or down in value. Since it's an offset to the time dimension, the phase is added to*t*in the basic equation before the sine function is applied:*f(t) = A sin(2πf·t + ϕ) + C*In this case,

*ϕ*is in units of radians. This equation is the complete form of the sine wave, and it describes every characteristic that a sine wave can have. Most of the time, the phase will be used this way in units of radians, but we can gain a little intuition by factoring out the 2π:*f(t) = A sin(2π(f·t + p)) + C*Now

*p*is the phase as a percentage of the period of the signal, with the period being the inverse of the frequency, i.e. the length of time to complete one cycle of the signal. If*p*= 0.5, then the signal would be shifted 50% of the signal's period. Here's what that looks like visually:There are a number of things to notice in this graph while clicking through it. First, a positive phase will shift the signal to the left,

*not*to the right. It's similar to frequency in that it has the opposite effect that you might expect because it is changing time before the rest of the function is applied. Adding a positive phase moves time forward, so it is as if we are looking through a window at the signal, and that window is moving to the right, just like the moving plots in my last post. In fact, that's exactly how I made those moving plots—by increasing the phase of the signals. Here's the JavaScript code for the sine wave animation:`function animate() {`

t += 2;

if (t >= range) t = 0;

sinewave.clear();

sinewave.lineStyle(2, 0x00bbdd, 1);

var y = amplitude*Math.sin(2.0*Math.PI*(t)/range);

sinewave.moveTo(0, y);

for (var x = 0; x <= 520; x+=10) {

y = amplitude*Math.sin(2.0*Math.PI*(x+t)/range);

sinewave.lineTo(x, y);

sinewave.moveTo(x, y);

}

renderer.render(stage);

requestAnimationFrame( animate );

}

The variable t is incremented every frame before the sine wave is redrawn. This causes it to look like the sine wave is moving to the left.Coming back to the different phases in the graph, the phase of 25% reproduces the cosine wave because that's exactly what the cosine is. It's the sine wave with a 90 degree (or 25%) phase shift. Next, the phase of 50% looks the same as the negative amplitude transform because of the symmetry of the sine wave, and the phase of 100% brings us back to the original sine wave because we've now shifted in time by one full period of the sine wave. Finally, the phase of -50% looks the same as 50% because they also differ by one full period, but the signals were shifted in opposite directions. Hopefully, this gives you a good feeling on how changes in phase affect a signal.

#### Adding Signals

The final transform we'll cover today is adding signals together. Sine waves with different parameters can be added together to represent any other signal, and such a sequence of sine waves is called a Fourier Series. Last time I alluded to the fact that the other basic signals could be constructed from a Fourier Series, so let's look at one of them. The periodic version of the step function, the square wave, is fairly easy to build up with a sum of sines. We start with a normal sine function:*f(t) = 4sin(πt)*Then we add more sine functions to it with odd multiples of the original frequency. We also need to scale down the amplitude of each successive sine wave by the same multiple of the frequency. The first three terms would be:

*f(t) = 4(sin(πt) + sin(3πt)/3 + sin(5πt)/5)*This is only an approximation of a square wave. To get the real thing, we'd have to continue the summation forever:

*f(t) = 4 ∑(n=1,3,5,…) sin(nπt)/n*That's a lot of terms. Here's what the first 20 terms look like as they are added together (just keep clicking):

As more and more terms are added to the summation, the signal looks more and more like a square wave. The steps get more vertical, and the other regions get flatter. You can see how it would take an awful lot of terms to make a true square wave, and that is why a square wave has infinite frequency content.

That about wraps it up. The complete sine function includes the four basic transforms of offset, amplitude, frequency, and phase, and a sequence of these sine functions can be added together to represent any other signal that exists. There are other transforms, but these are the fundamental ones. We'll get into some of the other ones in later posts. For next time we'll take a look at sampling theory so we can start doing some

*digital*signal processing.**Other DSP posts:**Basic Signals

Transforms

Sampling Theory

Averaging

Step Response of Averaging

Signal Variance

Edge Detection

Signal Envelopes

Frequency Measurement

The Discrete Fourier Transform

The DFT in Use

Spectral Peak Detection

FIR Filters

Frequency Detection

DC and Impulsive Noise Removal

## 0 Response to "Everyday DSP for Programmers: Transforms"

## Post a Comment