← All tutorials

Independent Component Analysis (ICA)

Unmixing signals recorded on several sensors.

On this page

Principal component analysis (PCA) and its cousin independent component analysis (ICA) both write data as a few directions times amounts; PCA asks for the directions of most spread, ICA for directions whose amounts are statistically independent, and non-negative matrix factorization (NMF) for parts that are never negative.

PCA1most spread,at right anglesICA22.2 whiten2.4 best angle2.5 FastICANMF3edges from 0,never negative
Figure 1. PCA 1 finds most spread, ICA 2 undoes mixing, NMF 3 adds parts.

ICA fits once, in batch, on a whole recording; after that, unmixing a new sample is subtracting the training mean, then one matrix multiply.

1 Three ways to factor data

Each method writes the data as a few blocks times amounts; they differ in what they ask of the blocks.

PCAICANMF
What it asks formost spreadindependent amounts, like two voices in one roomparts that only add, like notes in a spectrogram
Inputone data matrixseveral sensors, one column eachone non-negative data matrix
Sign of blocksanyanynever negative
Blocks at right angles?yesnot in generalnot in general

What PCA, ICA and NMF ask of the same data.

PCA is where ICA begins, since ICA starts by whitening, a step taught in Principal Component Analysis. NMF gets its own page, Non-negative Matrix Factorization, next in Classical ML.

2 Independent component analysis

2.1 Two microphones, two voices

Two people talk in one room, and two phones or meeting-room mics record them. Each mic hears its own blend of the two voices.

voice 1voice 2mic 1mic 22321
Figure 2. Two voices reach two mics, each arrow labelled with its weight.

Mic 1 hears 2 parts voice 1 and 3 parts voice 2, mic 2 hears 2 parts voice 1 and 1 part voice 2, and these four weights make the mixing matrix A. The weights come from Hyvärinen and Oja (2000), the ICA tutorial this page follows.

An illustrative toy with 4 samples keeps every step checkable by hand. Each voice is on (+1) or off (−1); off is −1, not 0, because each voice is centred on its own average, so its mean is zero. Every on and off pair appears once, so knowing one voice says nothing about the other. The same steps run on the speech-like and music-like stand-in signals of the tool in Section 2.7.

Sample 1Sample 2Sample 3Sample 4
Voice 1, s111−1−1
Voice 2, s21−11−1
Mic 15−11−5
Mic 231−1−3

The two voices and the two mic readings at each of the 4 samples.

Each mic adds up the voices, each weighted by its arrow in Figure 2.
x = As
Sample 1 has both voices on, (1, 1), so mic 1 reads 2 + 3 = 5 and mic 2 reads 2 + 1 = 3, the Sample 1 column of the table.
s the sources, the two voices at one instant.A the mixing matrix, the arrow weights of Figure 2.x what the mics record.

2.2 Whitening with PCA

ICA first whitens the mic readings, as taught in PCA variants. Whitening removes their correlation and sets every spread to 1, so only a rotation is left to find.

SampleMics xWhitened z̃
15, 31.411, −0.088
2−1, 1−0.088, −1.411
31, −10.088, 1.411
4−5, −3−1.411, 0.088
Spread along PCA axes17.062, 0.9381, 1

The four mic samples before and after whitening.

Neither whitened coordinate equals a voice: whitening removed correlation, not all of the mixing.

Turn the cloud to its PCA axes, then shrink or stretch each axis until its spread is 1. The result is round, so no direction stands out by spread anymore.
z = VTx
Measure each sample along the PCA directions; sample 1, (5, 3), measures 5.830 along the first and −0.085 along the second.
z̃ = Λ−1/2z
Divide each coordinate by the square root of its spread, so z̃j is zj divided by √λj; sample 1 becomes 5.830 divided by 4.131, which is 1.411, and −0.085 divided by 0.968, which is −0.088, row 1 of the table.
x the two mic readings at one instant, the thing being made round.V the PCA directions, the eigenvectors of the covariance C (rows 13, 7 and 7, 5); here V has columns (0.865, 0.502) and (0.502, −0.865), one PCA direction each; VT, the transpose, is V with rows and columns swapped, and VTx measures x along those directions.Λ the eigenvalues 17.062 and 0.938 on a diagonal, the spread along each PCA direction, and λj the spread along direction j.z̃ the whitened sample: every direction has variance 1 and no two coordinates are correlated. The card says PCA, then equalize the spreads.

Round is not separated; Section 2.3 says what is left to undo.

2.3 Undoing the mix

Independent means knowing one source tells you nothing about the other. The whitened samples are uncorrelated, yet z̃1 = 1.411 tells you z̃2 = −0.088 in the Section 2.2 table. Uncorrelated only rules out a straight-line relation; independent rules out every relation.

Each mic hears a fixed blend of the voices, and inverting the blend gives the voices back. After whitening, the unknown blend only turns or flips the round cloud, so with two mics the whole problem is one angle.
s = Wx, W = A−1
A−1 takes (5, 3) back to (1, 1), the last row of the inverse table after this card, which undoes the Section 2.1 card.
z̃ = Ms, MMT = I
M turns the voices (1, 1) into the whitened sample (1.411, −0.088), row 1 of the Section 2.2 table.
W = MTΛ−1/2VT
Whitening is the Section 2.2 card, and MT turns back; multiplied out this is A−1, so only M is unknown.
s the sources, the two voices at one instant.A the mixing matrix, the arrow weights of Figure 2.x what the mics record.W the unmixing matrix, the inverse of A, which turns mic readings back into voices.M the blend left after whitening; I is the identity, the matrix that changes nothing, so MMT = I says M only turns or flips. As the tutorial puts it, “whitening solves half of the problem of ICA” (section 5.2).
ValueWhat it shows
Blend left after whitening, M0.662, 0.750−0.750, 0.662a turn, no flip
M times its transpose1, 00, 1rows of length 1, at right angles
A−1−0.25, 0.750.5, −0.5undoes A
A−1 times A1, 00, 1undoing after mixing changes nothing
A−1 times sample 1, (5, 3)1, 1the voices of sample 1

What whitening leaves of the blend, and the inverse of A.

Whitening made the cloud round. ICA fixes each source to variance 1, and the on/off voices already have it, so the sources were round too; what is left can only turn or flip the cloud, as row 2 shows.

1sources2mixed by A3whitened4turned back
Figure 3. Sources 1, mixed 2, whitened 3 with sample 1 ringed, turned back 4.

Every rotation keeps the cloud round; Section 2.4 says which one is right.

2.4 Non-Gaussianity

Gaussian is another name for the bell curve, and non-Gaussian means shaped unlike a bell.

Adding independent signals usually makes a more bell-shaped signal, the central limit theorem, so a blend looks more like a bell than either source. Turn the whitened cloud until the output looks least like a bell, and that output is one source. It works only if at most one source is a bell (the tutorial, section 3.3).

For a signal y scaled to spread 1, kurtosis is the average of y4 minus 3. The FastICA steps of Section 2.5 use it, and the purple ring of Figure 7 is the size of the kurtosis in each direction.

1speech-likepeakcut off02music-like03mic 1 blendkurtosis 2.310
Figure 4. Illustrative stand-ins 1 2 and their mic 1 blend 3, over dashed bells.

Both signals are 2 s synthetic stand-ins made in NumPy and embedded in the page, syllables with pauses between them and plucked notes, so every number from them is illustrative. Recorded speech is spiky in the same way: its samples fit a Laplace curve, the spiky shape, better than a bell (Aroudi et al. 2015).

SignalAverage of y4Kurtosis
Bell30
On/off voice s11−2
Toy mic 11.852−1.148
Laplace curve, the fit to recorded speech63
Speech-like, illustrative11.038.03
Music-like, illustrative8.515.51
Mic 1 blend, illustrative5.312.31

Average of y4 and kurtosis of seven signals, each scaled to spread 1.

The toy is flat, the stand-ins are spiky, and blends sit closer to 0.

Each direction in the whitened plane is an angle θ from the first PCA axis, and reading the cloud along it gives one output signal.

−2−1.5−10°90°180°angle θkurtosis12341.4°131.4°
Figure 5. Toy kurtosis by angle θ is lowest at sources 1 2, not the PCA axis 3.

The on/off voices are flat, so here least bell-like means most negative; the spiky voices of Figure 7 peak instead.

Kurtosis is “very sensitive to outliers” (the tutorial, section 4.2.1), so FastICA's default uses log cosh, the logarithm of the hyperbolic cosine, a U-shaped curve; it is a smooth stand-in for y4 that grows slowly for large values, so one loud outlier cannot decide the answer.

SignalAverage of log coshGap from the bell
Bell0.3750
Toy along the PCA axis, 0°0.3900.015
Toy along a source, 41.4°0.4340.059

Average of log cosh for a bell and for the toy at two angles, spread 1.

Read the whitened cloud along a direction, score how far that output is from a bell, and keep the direction with the highest score; that output is one source.
y = wTz̃, ‖w‖ = 1
Read the whitened cloud along the direction w; with w = (1, 0), y is the first whitened coordinate, 1.411, −0.088, 0.088, −1.411.
kurt(y) = E[y4] − 3(E[y2])2
How far the output is from a bell: 0 for a bell, positive when spiky, negative when flat; toy mic 1 gives 1.852 − 3 = −1.148.
G(u) = log cosh u
Close to half of u2 for small u, and it grows like |u| for large u, so a loud outlier adds about its size, not its fourth power; along a source every toy value is 1 or −1, and log cosh 1 is 0.434.
J(y) ∝ [E[G(y)] − E[G(ν)]]2
The first average is the output’s, 0.390 along the PCA axis and 0.434 along a source; the second is the bell’s fixed 0.375; the square counts flat and spiky outputs alike. The gap is 0.015 along the PCA axis and 0.059 along a source, squared 0.0002 against 0.0035, so J is about 15 times larger at the source and peaks there.
w a direction of length 1; y is the output along it; θ, in Figures 5 and 7, is the angle of w from the first PCA axis.E[·] the average over samples, the expected value.kurt(y) the bell test; the second term is 3 when y has spread 1.ν a bell with the same spread as y, so E[G(ν)] is a fixed number, 0.375 for spread 1; G scores each value; J the robust non-Gaussianity score; ∝ means proportional to, equal up to a constant factor.cosh the hyperbolic cosine, the average of eu and e−u, a U-shaped curve.

Scanning every angle works with two mics; Section 2.5 finds the best angle in a few steps with any number of mics.

2.5 FastICA step by step

FastICA climbs J of Section 2.4 using g, the slope (derivative) of G. The slope of log cosh is tanh, the hyperbolic tangent, an S-curve from −1 to 1, and the slope of a quarter of y4 is y3, the kurtosis form of the first-step table and Figure 6. As w turns, the average of G(y) changes at the rate given by the average of z̃ g(y), the first term of the update; at the peak that average points straight along w. FastICA moves w to that average, minus the part a bell would give, and reaches a source in a few steps.

StepResult
Read along w = (1, 0), y = z̃11.411, −0.088, 0.088, −1.411
Cube each value2.812, −0.001, 0.001, −2.812
Average of z̃ y31.985, −0.123
Subtract 3w−1.015, −0.123
Rescale to length 1 (length 1.023)a line at 6.9 degrees

The first FastICA step in the kurtosis form, from w = (1, 0).

Only the line matters, because w and −w give the same source up to sign (the tutorial, section 6.1).

Figure 6. Each step turns w toward a dashed source line; bars show y.

Each step averages the cloud weighted by how strongly each point lies along w, removes what a bell would give, and rescales. For a bell-shaped cloud that average points straight back along w, as 3w for the cube, so it carries no news; subtracting it leaves only the turn toward a source. Repeat until w stops turning. It is an approximate Newton step, one that uses the slope of g to set its own size, so unlike gradient descent it needs no learning rate.
w+ = E[z̃ g(y) − g′(y) w]
Here y is the output of the first line of the Section 2.4 card, and E averages everything inside the brackets. The first term weights each whitened sample by g of its value along w, averaging to (1.985, −0.123) in the table; the second term averages to the part a bell would give, 3w = (3, 0); their difference (−1.015, −0.123) is the new direction.
w = w+‖w+‖
Divide w+ by its length ‖w+‖ so w has length 1 again; the first step divides by 1.023, which gives the line at 6.9 degrees in the table. Repeat until |woldTw| is about 1, when the line stops turning.
y the output along w, as in Section 2.4.g the slope of G, u3 in the kurtosis form and tanh in the log cosh form; repeating the step climbs J of Section 2.4 to its peak; the tanh form, scikit-learn's default, lands in 2 steps from the same start.g′ its slope: 3u2 for the cube, and the average of 3y2 is 3 on whitened data, so the second term is 3w, the row Subtract 3w.wold the line before the step; the stop test compares it with the new line. More sources repeat this once per source, keeping each new w at right angles to the ones found.

ICA undoes an instant blend across several sensors; Section 2.6 says what that assumes.

2.6 What the model assumes, and what ICA cannot recover

Basic ICA needs at least as many mics as sources (the tutorial, section 2.1). Extra mics are fine, since whitening drops the extra dimensions (the tutorial, sections 4.5 and 5.2); with fewer mics than sources the outputs stay blends.

ICA cannot recover the order, sign or scale of the sources (the tutorial, section 2.2). As in Section 2.3, each output is fixed to variance 1, and the order and sign can change between runs.

The model also assumes an instant blend with no time delays (the tutorial, section 1). A real room breaks this: a voice reaches each mic first by the direct path, then as quieter echoes off the walls a little later, so each mic hears a blurred sum of the voice over recent time, the voice convolved with the room’s impulse response. One number per arrow of Figure 2 cannot hold that, so x = As breaks, and since the echoes boost some frequencies and cut others, the blend differs at each frequency.

The standard fix is frequency-domain ICA (Smaragdis 1998): take the STFT of each mic and run ICA separately in each frequency bin, where the blend is again one matrix, of complex numbers. Each bin returns its voices in its own order, so the last step puts every bin in the same order, the permutation problem. For one recording in a room, NMF can model the reverberation itself. Anushiravani (2016, section 5.11) uses convolutive NMF, whose parts each span several time frames, on spectrograms to split a reverberant recording into the dry sound, the voice with no room echo, and a reverberation kernel, the room’s echo pattern over time.

2.7 Unmixing a cocktail party

Whitening leaves one rotation, and the angle that makes the outputs least like a bell separates the voices.

Figure 7. Illustrative stand-ins mixed by A; purple dots mark sources.

3 ICA in scikit-learn

Take any two mono WAV recordings of your own at one sample rate, for example a voice and a song, and mix them by the A of Section 2.1; X has one row per sample and one column per mic, the shape scikit-learn expects.

import numpy as np
import scipy.io.wavfile as wf
import sklearn.decomposition as sd

_, s1 = wf.read("voice.wav")
_, s2 = wf.read("song.wav")
n = min(len(s1), len(s2))
S = np.c_[s1[:n], s2[:n]]
A = np.array([[2, 3], [2, 1]])
X = S @ A.T

ica = sd.FastICA(2)
S_hat = ica.fit_transform(X)
A_hat = ica.mixing_
  1. 1Reads the two sources s1 and s2, a voice and a song, cut to the same length.
  2. 2Mixes them as in the Section 2.1 card, written per row as SAT.
  3. 3FastICA with 2 outputs; the defaults whiten to unit variance (Section 2.2) and use log cosh (Section 2.4). n_components is the one positional parameter.
  4. 4Fits and returns the sources (Section 2.4 and Section 2.5); A_hat estimates A up to column order, sign and scale (Section 2.6). With no seed the order and sign can change from run to run.

4 ICA in practice

FastICA centers and whitens for you. A good fit gives outputs that look far from a bell, and a convergence warning means max_iter ran out.

SettingRaise itLower itExtremesDefault
n_componentsmore outputs, up to the number of mics in the basic modelkeeps only the strongest whitened directions, which drops noise as PCA does1 keeps only the strongest whitened direction, one outputNone, all features
funA choice, not a scale. "logcosh" is the general choice, "exp" suits spiky sources, and "cube" is kurtosis."cube" is outlier sensitive"logcosh"
max_itermore iterations before it stopsmay stop before w settles, with a convergence warningmax_iter = 1 takes one update from its start, like step 1 of Figure 6; scikit-learn starts at random, the figure at w = (1, 0)200
tolstops sooner with a looser wtighter w, but more iterations, and it may hit max_itertol = 0 always runs to max_iter1e-4
algorithmA choice, not a scale. "parallel" finds every w at once, and "deflation" finds them one after another.a choice; no extreme"parallel"

FastICA settings in scikit-learn 1.9.1, what each one does, and its default.

CauseSymptomFix
Runs disagreeoutputs come back in a different order, sign or scalethe model cannot know them; match outputs to sources by correlation, fix signs by a convention, or set random_state
Gaussian sourcesno direction is least bell-like, and outputs stay blendsICA cannot help, since at most one source may be Gaussian
More sources than micsoutputs carry blendsthe basic model needs at least as many mics as sources; add mics or use a method built for that case
Reverberant roomthe recording has delays and echoesthe blend is not one matrix; run frequency-domain ICA, one ICA per STFT bin, then put the bins in one order (Section 2.6)

Four ways ICA goes wrong on a recording, what you see, and the fix.

When the data is one recording and everything is non-negative, NMF, on its own page next in Classical ML, asks a different question.

5 References

  1. Hyvärinen, A., and Oja, E. (2000). Independent component analysis: algorithms and applications. Neural Networks 13(4-5), 411-430. doi:10.1016/S0893-6080(00)00026-5. Figure 3 here is redrawn after its Figures 5, 6 and 10.
  2. Aroudi, A., et al. (2015). Speech signal modeling using multivariate distributions. EURASIP Journal on Audio, Speech, and Music Processing 2015:35. link.springer.com
  3. Anushiravani, R. (2016). “Example-based audio editing”. M.S. thesis, UIUC. hdl.handle.net/2142/95331. Its reverberation matching with convolutive NMF (section 5.11) is the source of the room sentence in Section 2.6.
  4. Smaragdis, P. (1998). Blind separation of convolved mixtures in the frequency domain. Neurocomputing 22(1-3), 21-34. sciencedirect.com. The source of frequency-domain ICA in Section 2.6.