2009 Blair Bohannan: Difference between revisions
imported>Psych 204 No edit summary |
imported>Psych 204 No edit summary |
||
| Line 28: | Line 28: | ||
[[File:sinusoidFftshift.jpg | Figure 2]] | [[File:sinusoidFftshift.jpg | Figure 2]] | ||
<br> | <br> | ||
In the 2-dimensional case, fftshift swaps quadrants 1 and 3, and also 2 and 4 of the Fourier transform. Thus, the low-frequency information is moved to the center of the image, with the [0,0] frequency point moving from matrix index [1,1] to [nPix/2+1,nPix/2+1]. The fftshifted Fourier transform of an image is what we are used to looking at when we talk about [http://en.wikipedia.org/wiki/K-space_%28MRI%29 k-space]. | In the 2-dimensional case, fftshift swaps quadrants 1 and 3, and also 2 and 4 of the Fourier transform. Thus, the low-frequency information is moved to the center of the image, with the [0,0] frequency point moving from matrix index [1,1] to [nPix/2+1,nPix/2+1]. The fftshifted Fourier transform of an image is what we are used to looking at when we talk about [http://en.wikipedia.org/wiki/K-space_%28MRI%29 k-space]. For the purpose of this tutorial, ''time domain'' is the 1-dimensional case of ''image domain'', and ''k-space'' is specifically the 2-dimensional case of ''frequency domain''. | ||
<br> | <br> | ||
Here is a 2-d Fourier transform without fftshift. Note the bright spots (high amplitudes) in the outer corners of the image; these are the low frequencies. | Here is a 2-d Fourier transform without fftshift. Note the bright spots (high amplitudes) in the outer corners of the image; these are the low frequencies. | ||
<br> | |||
[[File:2dFFT.jpg | Figure 3]] | [[File:2dFFT.jpg | Figure 3]] | ||
<br> | <br> | ||
Here is the same transform, with fftshift applied to it. Now the low-frequency, high-amplitude information is in the center of the image. | Here is the same transform, with fftshift applied to it. Now the low-frequency, high-amplitude information is in the center of the image. | ||
[[File:2dFFT_shifted.jpg | Figure 4]] | [[File:2dFFT_shifted.jpg | Figure 4]] | ||
<br> | |||
Note: fftshift should be used ''after'' performing the FFT. Example: | |||
:X=fftshift(fft(x)); | |||
=== Using ifftshift === | |||
The [http://www.mathworks.com/access/helpdesk/help/techdoc/ref/ifftshift.html ifftshift] function reverses the effect of fftshift (use this instead of performing fftshift twice). The ifftshift should occur before taking the ifft: | |||
:x=ifft(ifftshift(X)); | |||
== Cell III. Artifact 1: DC offset == | == Cell III. Artifact 1: DC offset == | ||
=== Derivation === | |||
The DC offset is a constant, positive shift in amplitude. It results in a large peak at frequency zero. This can be derived from the equation for the DFT. Recall that the DFT is defined as | The DC offset is a constant, positive shift in amplitude. It results in a large peak at frequency zero. This can be derived from the equation for the DFT. Recall that the DFT is defined as | ||
:<math>X_k=\sum_{n=0}^{N-1} x_n e^{-\frac{j 2 \pi}{N} k n}\quad \quad k = 0, \dots, N-1 </math> | :<math>X_k=\sum_{n=0}^{N-1} x_n e^{-\frac{j 2 \pi}{N} k n}\quad \quad k = 0, \dots, N-1 </math> | ||
| Line 45: | Line 54: | ||
We are interested in what is going on at frequency <math>k=0</math>. Assuming that the first term does not have some other DC artifact, we know that its zero-frequency component should be zero (this has to do with [https://ccrma.stanford.edu/~jos/mdft/Orthogonality_DFT_Sinusoids.html orthogonality of the DFT sinusoids]). Focusing on the second term, since we are considering frequency <math>k=0</math>, the exponential is equal to <math>1</math> for all <math>n</math>. Thus, the amplitude of the transform at this frequency is the sum of every term in the DC signal. | We are interested in what is going on at frequency <math>k=0</math>. Assuming that the first term does not have some other DC artifact, we know that its zero-frequency component should be zero (this has to do with [https://ccrma.stanford.edu/~jos/mdft/Orthogonality_DFT_Sinusoids.html orthogonality of the DFT sinusoids]). Focusing on the second term, since we are considering frequency <math>k=0</math>, the exponential is equal to <math>1</math> for all <math>n</math>. Thus, the amplitude of the transform at this frequency is the sum of every term in the DC signal. | ||
:<math>X_0=N \Delta</math> | :<math>X_0=N \Delta</math> | ||
=== One-dimensional example === | |||
Here is a DC signal and its Fourier transform. Note that the DC amplitude is 3 and the FFT length is 32, making the zero-frequency peak 96. | |||
<br> | |||
[[File:dcSignalAndFFT.jpg | Figure 5]] | |||
<br> | |||
Here is the same DC signal added to the sinusoid from the previous section. Note that while the sinusoid was previously centered around 0, now it is centered around 3, the DC amplitude. The Fourier transform of this signal is the Fourier transform from before, with the added peak at zero. | |||
<br> | |||
[[File:sinusoidDCOffset.jpg | Figure 6]] | |||
<br> | |||
=== Two-dimensional example === | |||
The same will be the case for 2-dimensional data. The image will have a bright spot in the center (at frequency [0,0]). In the next figure, the zebra image has been combined with a large DC offset and frequency transformed. The original Fourier transform has been subtracted from this artifact Fourier transform, and we are left with only the bright spot. | |||
<br> | |||
[[File:2dFT_DCOffset.jpb | Figure 7]] | |||
== Cell IV. Removing time-domain DC offset == | |||
Two quick solutions come to mind for removing the time-domain (or image-domain) DC offset: | |||
* Subtract the mean of the signal from every sample; | |||
* Use a narrow [http://en.wikipedia.org/wiki/Highpass_filter highpass filter] to remove the zero frequency component. | |||
<br> | |||
Here is the 1-dimensional example from the previous section. The zero-frequency component has simply been set to zero (so it has effectively been filtered out). The IFFT of this new transform has shifted the original signal back down. | |||
[[File:removeZeroFreq.jpg | Figure 8]] | |||
== Cell V. Frequency-domain DC offset == | |||
=== Derivation === | |||
DC offset can occur in the frequency domain as well, with similar effects. Note, however, that the equation for the IDFT contains a scaling factor <math> \frac{1}{N}</math> before the summation: | |||
:<math>x_n=\frac{1}{N} \sum_{k=0}^{N-1} X_k e^{\frac{j 2 \pi}{N} k n}\quad \quad n=0, \dots,N-1</math> | |||
(this would be <math>\frac{1}{N^2}</math> for the 2-dimensional case) | |||
<br> | |||
Therefore, the peak at time <math>n=0</math> will simply be the DC amplitude <math>\Delta</math>, not <math> N \Delta</math>. | |||
=== One-dimensional example === | |||
Here is the frequency-domain offset for a 1-dimensional signal. The top subplot shows the sinusoid's Fourier transform, shifted up by a constant amount. The bottom subplot shows the inverse Fourier transform. The signal at time 0 now has the DC amplitude added to what was already present. | |||
<br> | |||
[[File:fdDCOffset.jpg | Figure 9]] | |||
<br> | |||
As in the time-domain case, this artifact can be removed by subtracting the mean of the Fourier transform from every element (in the Fourier transform). Note in the figure below, though, that doing so removes the ''entire'' signal at time zero, so the original signal at the time needs to be recovered (this can be done with some form of [http://en.wikipedia.org/wiki/Interpolation interpolation]). | |||
<br> | |||
[[File:subtrFreqMeans.jpg | Figure 10]] | |||
== Cell VI. Frequency-domain DC offset - two-dimensional case == | |||
Here is an example of the effect of a k-space DC offset on a reconstructed image. The original zebra image has been reduced to a smaller pixel set so that the artifact is easier to see. The top two plots show the original image and its Fourier transform. The bottom two plots show the reconstruction of the original Fourier transform and the reconstruction of a Fourier transform that has a large DC offset. Note that the high amplitude of the bright spot has obscured the rest of the image. | |||
<br> | |||
[[File:2dFTDCOffset.jpg | Figure 11]] | |||
Revision as of 09:11, 9 December 2009
Back to Psych 204 Projects 2009
Project Title - MATLAB image processing tutorial
This page accompanies a MATLAB tutorial which simulates some artifacts that might be found in MR images. Their causes, and in some cases corrections, are demonstrated in the code. One- and two-dimensional examples are provided.
Image artifacts in this tutorial include DC Offset, Quadrature Ghosting, RF Noise, Gradient, and Frequency-Sampling Jitter. Information on the first four topics can be found in Hornak, Joseph P. The Basics of MRI, Chapter 11. More information on the mathematical derivations can be found in Smith, Julius O. Mathematics of the Discrete Fourier Transform (DFT). A full reference list can be found at the end of this page.
About the tutorial
This tutorial consists of a MATLAB (.m) script and some image files. Unzip the files to a folder and either add to path or set the MATLAB workspace to this folder before starting the tutorial.
The tutorial uses cells, and each section below corresponds to a cell in the tutorial. To evaluate a cell in the tutorial, first navigate into it. Ctrl+Enter evaluates the current cell, and Ctrl+Shift+Enter evaluates and moves to the next cell. Variables are re-used throughout the tutorial, so start at the first cell and work through the tutorial in order.
The tutorial package contains a selection of image files. Those titled "stimulus##.tif" (including the one used on this page) are from an image set prepared by Roozbeh Kiani. The associated publication can be found here. Used with permission.
Tutorial
Cell I. Loading images for analysis
The first cell simply loads an image, converts it to grayscale, and sets the size of the image. nPix, the variable setting the number of pixels in the x and y dimensions, should be a power of 2 for faster FFT calculation.
This figure contains the image in grayscale. It has nPix x nPix pixels (in this case, 256 x 256).
Cell II. Using fftshift
The fftshift function is helpful for visualing Fourier transforms in a familiar format. In the 1-dimensional N-point Fourier transform (like the transform of an audio signal), it takes points N/2+1 to N (containing the Nyquist and negative frequency components) and moves them to the beginning of the array. See Oppenheim and Schafer, Chapter 4, if you want to know why this can happen. Thus, the fftshifted function causes the poisitive and negative frequencies to be symmetric about the zero-frequency point. This being the case, it's helpful to keep in mind that the zero-frequency sample is at sample N/2+1.
Here is a 1-dimensional example. The red dot is the zero-frequency component of the transform of the sinusoid in the top subplot.
In the 2-dimensional case, fftshift swaps quadrants 1 and 3, and also 2 and 4 of the Fourier transform. Thus, the low-frequency information is moved to the center of the image, with the [0,0] frequency point moving from matrix index [1,1] to [nPix/2+1,nPix/2+1]. The fftshifted Fourier transform of an image is what we are used to looking at when we talk about k-space. For the purpose of this tutorial, time domain is the 1-dimensional case of image domain, and k-space is specifically the 2-dimensional case of frequency domain.
Here is a 2-d Fourier transform without fftshift. Note the bright spots (high amplitudes) in the outer corners of the image; these are the low frequencies.
Here is the same transform, with fftshift applied to it. Now the low-frequency, high-amplitude information is in the center of the image.
Note: fftshift should be used after performing the FFT. Example:
- X=fftshift(fft(x));
Using ifftshift
The ifftshift function reverses the effect of fftshift (use this instead of performing fftshift twice). The ifftshift should occur before taking the ifft:
- x=ifft(ifftshift(X));
Cell III. Artifact 1: DC offset
Derivation
The DC offset is a constant, positive shift in amplitude. It results in a large peak at frequency zero. This can be derived from the equation for the DFT. Recall that the DFT is defined as
where is the time signal at times and is the Fourier transform at frequency indices . The DFT of a signal plus a constant DC offset would then be
Since this is a linear system, we can separate the DC component from the original signal.
We are interested in what is going on at frequency . Assuming that the first term does not have some other DC artifact, we know that its zero-frequency component should be zero (this has to do with orthogonality of the DFT sinusoids). Focusing on the second term, since we are considering frequency , the exponential is equal to for all . Thus, the amplitude of the transform at this frequency is the sum of every term in the DC signal.
One-dimensional example
Here is a DC signal and its Fourier transform. Note that the DC amplitude is 3 and the FFT length is 32, making the zero-frequency peak 96.
Here is the same DC signal added to the sinusoid from the previous section. Note that while the sinusoid was previously centered around 0, now it is centered around 3, the DC amplitude. The Fourier transform of this signal is the Fourier transform from before, with the added peak at zero.
Two-dimensional example
The same will be the case for 2-dimensional data. The image will have a bright spot in the center (at frequency [0,0]). In the next figure, the zebra image has been combined with a large DC offset and frequency transformed. The original Fourier transform has been subtracted from this artifact Fourier transform, and we are left with only the bright spot.
Figure 7
Cell IV. Removing time-domain DC offset
Two quick solutions come to mind for removing the time-domain (or image-domain) DC offset:
- Subtract the mean of the signal from every sample;
- Use a narrow highpass filter to remove the zero frequency component.
Here is the 1-dimensional example from the previous section. The zero-frequency component has simply been set to zero (so it has effectively been filtered out). The IFFT of this new transform has shifted the original signal back down.
Cell V. Frequency-domain DC offset
Derivation
DC offset can occur in the frequency domain as well, with similar effects. Note, however, that the equation for the IDFT contains a scaling factor before the summation:
(this would be for the 2-dimensional case)
Therefore, the peak at time will simply be the DC amplitude , not .
One-dimensional example
Here is the frequency-domain offset for a 1-dimensional signal. The top subplot shows the sinusoid's Fourier transform, shifted up by a constant amount. The bottom subplot shows the inverse Fourier transform. The signal at time 0 now has the DC amplitude added to what was already present.
As in the time-domain case, this artifact can be removed by subtracting the mean of the Fourier transform from every element (in the Fourier transform). Note in the figure below, though, that doing so removes the entire signal at time zero, so the original signal at the time needs to be recovered (this can be done with some form of interpolation).
Cell VI. Frequency-domain DC offset - two-dimensional case
Here is an example of the effect of a k-space DC offset on a reconstructed image. The original zebra image has been reduced to a smaller pixel set so that the artifact is easier to see. The top two plots show the original image and its Fourier transform. The bottom two plots show the reconstruction of the original Fourier transform and the reconstruction of a Fourier transform that has a large DC offset. Note that the high amplitude of the bright spot has obscured the rest of the image.