Exercises (9)

Assigned: March 26, 2013
Due: April 4, 2013

Fourier Spectrum and Power Spectrum

Compute and display the Fourier transform (using ListPlotComplex[…]) and the power spectrum (using ListPlot[Log[Abs[…]]] or ListPlot[Abs[…]]) for the following data sequences.

nX = 2^10 (* 1k Samples *)

x1 = Table[Mod[θ 0.2123,1], {θ, 0, nX -1}];

x2 = Table[If[Mod[θ 0.2123,2π] < π, 1, -1], {θ, 0, nX -1}];

x3 = Table[Sqrt[Abs[Sin[θ 0.2123]]], {θ, 0, nX -1}];

x1a = Table[Mod[θ 0.02123,1], {θ, 0, nX -1}];

x2a = Table[If[Mod[θ 0.02123,2π] < π, 1, -1], {θ, 0, nX -1}];

x3a = Table[Sqrt[Abs[Sin[θ 0.02123]]], {θ, 0, nX -1}];

If I Had a Million Dollars (In-Class)

Using the same procedure used in class, import the sound track "Million_Dollar_64kb.aif" and create a frequency spectrogram.

Solution

Directory[]

SetDirectory["~/Desktop/"]

s = Import["Million_Dollar_64kb.aif"]

Show[s]

rate = Part[s,1,2](* Sample Rate: Samples Per Second *)

sData = Drop[Part[s,1,1,1],1]; (* Make exactly 2^16 = 64 kS *)

nData = Length[sData]

2^16

tfd[x_List,windowSize_:200, padSize_:1] := Module[{i,j,n,pad, window, xw, xf, result},
n = Length[x];
pad = Table[0, {i, 1, windowSize padSize}];
window = Table[If[i ≤ windowSize/2, (i-1) ,(windowSize - i)], {i, 1, windowSize}]/(0.5 windowSize);
result = {};
Do[xw = Take[x, {j,j+windowSize-1}]; 
xw = xw - (Plus @@ xw)/n; (* remove average *)
xw = Join[pad , (xw window), pad];
xf = Fourier[xw];
result = Append[result, Abs[Drop[xf,-Floor[Length[xf]/2]]]],
{j, 1, n-windowSize -1, windowSize/2}];
Transpose[result]]

sTFD = tfd[sData];

Print["[Max, Min] = ",Max[sTFD],", ",Min[sTFD]];ListDensityPlot[sTFD, PerformanceGoal -> "Quality",Mesh->False, DataRange -> {{0, nData/rate},{0,rate/2}}]

Band Pass Filter

Apply a band-pass filter to the signal above, and re-display the spectrogram.

Information Entropy

Introductory Comments

An important measure in communitcation and data theory is called "information entropy". 

Entropy is a concept first developed for statistical physics. It is a measure of the disorder of a system. When dealing with digital data, entropy measures the disorder (or complexity) of the data. When the entropy is small, the data is not disordered, and only a few digital values exist within the entire data set. For example, if you had 1000 data points with values all equal to 1, then the information entropy would be exactly zero. The data is not at all disordered, and (theoretically) you could represent the information contained within this data set by one bit. When the entropy is large, the data is very disordered, and many digital values exist in the data set.

The Mathematica function for information entropy is written as

iEntropy[list_List]:=(-Plus@@N[#1 Log[2,#1]]&)[((First[Transpose[Reverse[Sort[Tally[list]],2]]])/(Length[list]))]

This function computes the sum of the logs of the "probability" of each value in the data.

For example, a simple list with high entropy (and requiring at least 100 bits of data) is

i1=RandomInteger[{1,10},30]

Tally[i1]

Sort[Tally[i1]]

ListPlot[Sort[Tally[i1]], Filling-> Axis, AxesLabel -> {"Value", "Count"}]

Reverse[Sort[Tally[i1]],2]

First[Transpose[Reverse[Sort[Tally[i1]],2]]]/Length[i1]

Plus @@ %

Print["Entropy = ",iEntropy[i1], "; Bits Required = ", iEntropy[i1] Length[i1]];

Entropy[ ] is also a built-in function in Mathematica…

Entropy[2,i1] // N

For another example, a simple list with low entropy  (and requiring at least 30 bits of data) is

i2 = Table[If[i<15,1, 2], {i, 1, 30}]

Reverse[Sort[Tally[i2]],2]

Print["Entropy = ",iEntropy[i2], "; Bits Required = ", iEntropy[i2] Length[i2]];

As the data becomes more orderly, the information entropy decreases.

For two-dimensional data, your must first Flatten[…] the data list before using iEntropy[…]. This converts the multiple-dimensional data to a one-dimensional list having the form acceptable to Frequencies[…].

You should be very aware of the difference between Real and Integer numbers. When you import an audio file (.aif), the integer (8-bit) data is converted to Real numbers. The data is still "quantized" into 256 different real numbers. The iEntropy[…] function works with quantized real numbers as well as integers. However, once you transform this data (using either a FFT or a DCT), the transform will not be quantized. The information entropy will grow considerably.

Therefore, in examining your data whether you use JPEG - weighted quantization or a uniform quantization, you must first quantize your data before you measure the information content with iEntropy[…].

Assignment

What is the Entropy of the six signals in the first part of this assignment?

What is the Entropy of the sound track "Million_Dollar_64kb.aif" before and after you applied the bandpass filter?

For each of these problem, make sure that you quantize your data before applying the Entropy[…] function.