Main Content

stftmag2sig

Signal reconstruction from STFT magnitude

Since R2020b

Description

x = stftmag2sig(s,nfft) returns a reconstructed time-domain real signal, x, estimated from the Short-Time Fourier Transform (STFT) magnitude, s, based on the Griffin-Lim algorithm. The function assumes s was computed using discrete Fourier transform (DFT) length nfft.

example

x = stftmag2sig(s,nfft,fs) returns the reconstructed signal assuming that s was sampled at rate fs.

example

x = stftmag2sig(s,nfft,ts) returns the reconstructed signal assuming that s was sampled with sample time ts.

x = stftmag2sig(___,Name=Value) specifies additional options using name-value arguments. Options include, among others, the FFT window and the method to specify initial phases. These arguments can be added to any of the previous input syntaxes. For example, FrequencyRange="onesided",InitializePhaseMethod="random" specifies that the signal is reconstructed from a one-sided STFT with random initial phases.

example

[x,t,info] = stftmag2sig(___) also returns the time instants at which the signal is reconstructed and a structure that contains information about the reconstruction process.

Examples

collapse all

Consider 512 samples of a sinusoid with a normalized frequency of π/60 rad/sample and a DC value of 1. Compute the STFT of the signal.

n = 512;

x = cos(pi/60*(0:n-1)')+1;

S = stft(x);

Reconstruct the sinusoid from the magnitude of the STFT. Plot the original and reconstructed signals.

xr = stftmag2sig(abs(S),size(S,1));

plot(x)
hold on
plot(xr,"--",LineWidth=2)
hold off
legend("Original","Reconstructed")

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent Original, Reconstructed.

Repeat the computation, but now pad the signal with zeros to decrease edge effects.

xz = circshift([x; zeros(n,1)],n/2);

Sz = stft(xz);
xr = stftmag2sig(abs(Sz),size(Sz,1));

xz = xz(n/2+(1:n));
xr = xr(n/2+(1:n));

plot(xz)
hold on
plot(xr,"--",LineWidth=2)
hold off
legend("Original","Reconstructed")

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent Original, Reconstructed.

Repeat the computation, but now decrease edge effects by assuming that x is a segment of a signal twice as long.

xx = cos(pi/60*(-n/2:n/2+n-1)')+1;

Sx = stft(xx);
xr = stftmag2sig(abs(Sx),size(Sx,1));

xx = xx(n/2+(1:n));
xr = xr(n/2+(1:n));

plot(xx)
hold on
plot(xr,"--",LineWidth=2)
hold off
legend("Original","Reconstructed")

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent Original, Reconstructed.

Load an audio signal that contains two decreasing chirps and a wideband splatter sound. The signal is sampled at 8192 Hz. Plot the STFT of the signal. Divide the waveform into 128-sample segments and window the segments using a Hamming window. Specify 64 samples of overlap between adjoining segments and 1024 DFT points.

load splat
ty = (0:length(y)-1)/Fs;

% To hear, type sound(y,Fs)

wind = hamming(128);
olen = 64;
nfft = 1024;

stft(y,Fs,Window=wind,OverlapLength=olen,FFTLength=nfft)

Figure contains an axes object. The axes object with title Short-Time Fourier Transform, xlabel Time (s), ylabel Frequency (kHz) contains an object of type image.

Compute the magnitude and phase of the STFT.

s = stft(y,Fs,Window=wind,OverlapLength=olen,FFTLength=nfft);

smag = abs(s);
sphs = angle(s);

Reconstruct the signal based on the magnitude of the STFT. Use the same parameters that you used to compute the STFT. By default, stftmag2sig initializes the phases to zero and uses 100 optimization iterations.

[x,tx,info] = stftmag2sig(smag,nfft,Fs, ...
    Window=wind,OverlapLength=olen);

% To hear the reconstruction, type sound(x,Fs)

Plot the original and reconstructed signals. For better comparison, shift the reconstructed signal vertically and to the right.

plot(ty,y,tx+500/Fs,x+1)
legend("Original","Reconstructed",Location="best")

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent Original, Reconstructed.

Output the relative improvement toward convergence between the last two iterations.

impr = info.Inconsistency
impr = 
0.0579

Improve the reconstruction by doubling the number of optimization iterations and setting the initial phases to the actual phases from the STFT. Plot the original and reconstructed signals. For better comparison, plot the negative of the reconstructed signal and shift it vertically and to the right.

[x,tx,info] = stftmag2sig(smag,nfft,Fs, ...
    Window=wind,OverlapLength=olen, ...
    MaxIterations=200,InitialPhase=sphs);

% To hear the reconstruction, type sound(x,Fs)

plot(ty,y,tx+500/Fs,-x+1)
legend("Original","Reconstructed",Location="best")

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent Original, Reconstructed.

Output the relative improvement toward convergence between the last two iterations.

impr = info.Inconsistency
impr = 
2.0848e-16

Generate a signal sampled at 5 kHz for 4 seconds. The signal consists of a set of pulses of decreasing duration separated by regions of oscillating amplitude and fluctuating frequency with an increasing trend.

fs = 5000;
t = 0:1/fs:4-1/fs;

x = 10*besselj(0,1000*(sin(2*pi*(t+2).^3/60).^5));

Compute and plot the short-time Fourier transform of the signal. Divide the signal into 128-sample segments and window the segments using a Hann window. Specify 96 samples of overlap between adjoining segments and 128 DFT points.

win = hann(128);
olen = 96;
nfft = 128;
stft(x,fs,Window=win,OverlapLength=olen,FFTLength=nfft)

Figure contains an axes object. The axes object with title Short-Time Fourier Transform, xlabel Time (s), ylabel Frequency (kHz) contains an object of type image.

Compute the magnitude and phase of the STFT.

s = stft(x,fs,Window=win,OverlapLength=olen,FFTLength=nfft);

smag = abs(s);
sphs = angle(s);

Reconstruct the signal based on the magnitude of the STFT. Use the same parameters that you used to compute the STFT. Instead of using the default Griffin-Lim algorithm, use the gradient descent method with the ADAM optimizer.

[xrec,trec,info] = stftmag2sig(smag,nfft,fs, ...
    Window=win,Method="gd",Optimizer="adam");

Plot the original and reconstructed signals. For better comparison, shift the reconstructed signal vertically so both are visible.

plot(t,x,trec,xrec+12)
legend("Original","Reconstructed",Location="northwest")
ylim([-5 27])

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent Original, Reconstructed.

Compute the mean-squared error between the original signal and the reconstruction.

sum((x-xrec').^2)/length(x)
ans = 
1.3620

For purposes of reproducibility, set the random seed to the default value. Generate 160 samples of a two-channel sinusoid. Add noise to each channel.

  • The first channel has unit amplitude and a normalized sinusoid frequency of π/4 rad/sample.

  • The second channel has unit amplitude and a normalized sinusoid frequency of π/2 rad/sample.

Compute the STFT of the signal. Divide the signal into 32-sample segments and window the segments using a Hann window.

rng default
N = 160;
t = 0:N-1;
win = hann(32);
signal = cos(pi./[4;2]*t)'+randn(N,2)/5;

s = stft(signal,Window=win);

Reconstruct the signal from the magnitude of the STFT. Use the gradient descent method with the L-BFGS optimizer. The L-BFGS optimizer automatically selects the step size for each iteration. Track the normalized inconsistency of each channel during the optimization process.

[x,tx,info] = stftmag2sig(abs(s), ...
    size(s,1),Window=win, ...
    Method="gd",Optimizer="lbfgs", ...
    Display=true);
#Iteration  |  Normalized Inconsistency  
      1     |  1.2078e+00 1.2042e+00 
     20     |  3.2348e-02 9.6221e-03 
     40     |  1.9969e-02 6.8350e-03 
     60     |  2.5463e-03 1.2099e-03 
     80     |  2.4790e-03 8.4016e-04 
    100     |  5.4774e-03 7.1804e-04 

Decomposition process stopped.
The number of iterations reached the specified "MaxIterations" value of 100.

Plot the original and reconstructed signals.

tiledlayout(2,1)
nexttile
plot(t,signal(:,1))
hold on
plot(tx,x(:,1),"--",LineWidth=2)
hold off
title("First Channel")
legend("Original","Reconstructed", ...
    Location="northoutside")
nexttile
plot(t,signal(:,2))
hold on
plot(tx,x(:,2),"--",LineWidth=2)
hold off
title("Second Channel")

Figure contains 2 axes objects. Axes object 1 with title First Channel contains 2 objects of type line. These objects represent Original, Reconstructed. Axes object 2 with title Second Channel contains 2 objects of type line.

Output the relative improvement toward convergence between the last two iterations.

info.Inconsistency
ans = 1×2

    0.0055    0.0007

Input Arguments

collapse all

STFT magnitude, specified as a matrix or a 3-D array. s must correspond to a real-valued signal.

  • When s corresponds to a single-channel signal, specify s as a matrix.

  • When s corresponds to a multichannel signal, specify s as a 3-D array, where the third dimension corresponds to the channels.

Example: abs(stft(sin(pi/2*(0:255)),FFTLength=128)) specifies the STFT magnitude of a sinusoid.

Example: abs(stft(chirp(0:1/1e3:1,25,1,50))) specifies the STFT magnitude of a chirp sampled at 1 kHz.

Example: abs(stft(cos(pi./[4;2]*(0:159))'+randn(160,2)/5)) specifies the STFT magnitude of a two-channel sinusoid with noise.

Data Types: single | double

Number of DFT points, specified as a positive integer scalar. This argument is always required.

Data Types: single | double

Sample rate, specified as a positive numeric scalar.

Sample time, specified as a duration scalar. Specifying ts is equivalent to setting a sample rate fs = 1/ts.

Example: seconds(1) is a duration scalar representing a 1-second time difference between consecutive signal samples.

Data Types: duration

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: FrequencyRange="onesided",InitializePhaseMethod="random",Method="gd" specifies that the signal is reconstructed from a one-sided STFT with random initial phases using the gradient descent method.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: "FrequencyRange","onesided","InitializePhaseMethod","random" specifies that the signal is reconstructed from a one-sided STFT with random initial phases.

Inconsistency display option, specified as a numeric or logical 1 (true) or 0 (false). If this option is set to true, stftmag2sig displays the normalized inconsistency after every 20 optimization iterations. The function also displays stopping information at the end of the run.

Data Types: logical

Frequency range of STFT magnitude, specified as one of these:

  • "centered" — Treat s as the magnitude of a two-sided, centered STFT. If nfft is even, then s is taken to have been computed over the interval (–π, π] rad/sample. If nfft is odd, then s is taken to have been computed over the interval (–π, π) rad/sample. If you specify time information, then the intervals are (–fs, fs/2] cycles/unit time and (–fs, fs/2) cycles/unit time, respectively, where fs is the sample rate.

  • "twosided" — Treat s as the magnitude of a two-sided STFT computed over the interval [0, 2π) rad/sample. If you specify time information, then the interval is [0, fs) cycles/unit time.

  • "onesided" — Treat s as the magnitude of a one-sided STFT. If nfft is even, then s is taken to have been computed over the interval [0, π] rad/sample. If nfft is odd, then s is taken to have been computed over the interval [0, π) rad/sample. If you specify time information, then the intervals are [0, fs/2] cycles/unit time and [0, fs/2) cycles/unit time, respectively, where fs is the sample rate.

Data Types: char | string

Inconsistency tolerance of reconstruction process, specified as a positive scalar. The reconstruction process stops when the Normalized Inconsistency is lower than the tolerance.

Data Types: single | double

Phase initialization, specified as "zeros" or "random". Specify only one of InitializePhaseMethod or InitialPhase.

  • "zeros" — The function initializes the phases as zeros.

  • "random" — The function initializes the phases as random numbers distributed uniformly in the interval [–π, π].

Data Types: char | string

Initial phases, specified as a real numeric matrix or 3-D array. Elements of InitialPhase must be in the range [–π, π]. InitialPhase must have the same size as s. Specify only one of InitializePhaseMethod or InitialPhase.

Example: angle(stft(randn(1000,1))) specifies the phases of the short-time Fourier transform of a random signal.

Example: 2*pi*(rand(size(stft(randn(1000,1))))-1/2) specifies a matrix of random phases distributed uniformly in the interval [–π, π]. The matrix has the same size as the short-time Fourier transform of a random signal.

.

Data Types: single | double

Input time dimension, specified as one of these:

  • "acrosscolumns" — The function assumes that the time dimension of s is across the columns and the frequency dimension is down the rows.

  • "downrows" — The function assumes that the time dimension of s is down the rows and the frequency dimension is across the columns.

Data Types: char | string

Maximum number of optimization iterations, specified as a positive integer scalar. The reconstruction process stops when the number of iterations is greater than MaxIterations.

Data Types: single | double

Signal reconstruction algorithm, specified as one of these:

  • "gla" — The original reconstruction algorithm, proposed by Griffin and Lim and described in [1].

  • "fgla" — A fast Griffin-Lim algorithm proposed by Perraudin, Balazs, and Søndergaard and described in [2].

  • "legla" — A fast algorithm proposed by Le Roux, Kameoka, Ono, and Sagayama and described in [3].

  • "gd" — A gradient descent method, proposed by Ji and Tie and described in [4]. (since R2023b)

Note

To specify the gradient descent method, you must have a Deep Learning Toolbox™ license.

Data Types: char | string

Since R2023b

Optimizer to use in the gradient descent method, specified as one of these:

  • "sgdm" — Stochastic gradient descent with momentum (SGDM) optimizer

  • "adam" — Adaptive moment estimation (ADAM) optimizer

  • "rmsprop" — Root-mean-square propagation (RMSProp) optimizer

  • "lbfgs" — Limited-memory Broyden–Fletcher–Goldfarb–Shanno (L-BFGS) optimizer

This argument applies only when Method is set to "gd".

Data Types: char | string

Number of overlapped samples between adjoining segments, specified as a positive integer smaller than the length of Window. Successful signal reconstruction requires OverlapLength to match the number of overlapped segments used to generate the STFT magnitude. If you omit OverlapLength or specify it as empty, it is set to the largest integer less than or equal to 75% of the window length, which is 96 samples for the default Hann window.

Data Types: double | single

Since R2023b

Step size for iterative updates used in the gradient descent method, specified as a positive scalar. This argument is valid only when Method is set to "gd" and Optimizer is not "lbfgs".

Data Types: single | double

Truncation order for "legla" update rule, specified as a positive integer. This argument applies only when Method is set to "legla" and controls the number of phase values updated in each iteration of that method. If not specified, TruncationOrder" is determined using an adaptive algorithm.

Data Types: single | double

Update parameter for the fast Griffin-Lim algorithm, specified as a positive scalar. This argument applies only when Method is set to "fgla" and specifies the parameter for that method's update rule.

Data Types: single | double

Spectral window, specified as a vector. Successful signal reconstruction requires Window to match the window used to generate the STFT magnitude. If you do not specify the window or specify it as empty, the function uses a periodic Hann window of length 128. The length of Window must be greater than or equal to 2.

For a list of available windows, see Windows.

Example: hann(128,"periodic") and (1-cos(2*pi*(128:-1:1)'/128))/2 both specify the default window used by stftmag2sig.

Data Types: double | single

Output Arguments

collapse all

Reconstructed time-domain signal, returned as a vector or a matrix.

Note

If you want x and s to be the same length, the value of (length(x)-noverlap)/(length(window)-noverlap) must be an integer. Use Window to specify the length of window and OverlapLength to specify noverlap.

Time instants at which the signal is reconstructed, returned as a vector.

Reconstruction process information, returned as a structure containing these fields:

  • ExitFlag — Termination flag.

    • A value of 0 indicates the algorithm stopped when it reached the maximum number of iterations.

    • A value of 1 indicates the algorithm stopped when it met the relative tolerance.

  • NumIterations — Total number of iterations.

  • Inconsistency — Average relative improvement toward convergence between the last two iterations.

  • ReconstructedPhase — Reconstructed phase at the last iteration.

  • ReconstructedSTFT — Reconstructed short-time Fourier transform at the last iteration.

More About

collapse all

Short-Time Fourier Transform

The short-time Fourier transform (STFT) is used to analyze how the frequency content of a nonstationary signal changes over time. The magnitude squared of the STFT is known as the spectrogram time-frequency representation of the signal. For more information about the spectrogram and how to compute it using Signal Processing Toolbox™ functions, see Spectrogram Computation with Signal Processing Toolbox.

The STFT of a signal is computed by sliding an analysis window g(n) of length M over the signal and calculating the discrete Fourier transform (DFT) of each segment of windowed data. The window hops over the original signal at intervals of R samples, equivalent to L = MR samples of overlap between adjoining segments. Most window functions taper off at the edges to avoid spectral ringing. The DFT of each windowed segment is added to a complex-valued matrix that contains the magnitude and phase for each point in time and frequency. The STFT matrix has

k=NxLML

columns, where Nx is the length of the signal x(n) and the ⌊⌋ symbols denote the floor function. The number of rows in the matrix equals NDFT, the number of DFT points, for centered and two-sided transforms and an odd number close to NDFT/2 for one-sided transforms of real-valued signals.

The mth column of the STFT matrix X(f)=[X1(f)X2(f)X3(f)Xk(f)] contains the DFT of the windowed data centered about time mR:

Xm(f)=n=x(n)g(nmR)ej2πfn.

Figure shows the short time Fourier transform sequence. On top, there is a signal in time domain x(n). Segmentation windowing follows below, partitioning x(n) into segments. Upon applying the Fourier transform to the signal segments, they altogether form the squared of the absolute value of X(f), the time-frequency domain equivalent of x(n).

Normalized Inconsistency

Normalized inconsistency measures the improvement toward convergence of the reconstruction process in successive optimization iterations.

The definition of normalized inconsistency depends on the signal reconstruction algorithm Method.

  • When Method is "gla", "fgla", or "legla", the inconsistency is defined as

    Inconsistency=STFT(ISTFT(sest))sestsest,

    where sest is the complex short-time Fourier transform estimated at each iteration, the brackets denote the matrix norm, STFT denotes the short-time Fourier transform, and ISTFT denotes its inverse.

  • When Method is "gd", the inconsistency is defined as

    Inconsistency=sest,isest,i1s,

    where sest,i is the estimated complex short-time Fourier transform estimated at the ith iteration, and s is the input short-time Fourier transform magnitude.

stftmag2sig uses the MATLAB® function norm to compute matrix norms. For more information about the STFT and its inverse, see Short-Time Fourier Transform and Inverse Short-Time Fourier Transform.

Tips

  • If you are using the gradient descent method and the reconstruction is not satisfactory, set Display to true. Observe the inconsistency during iterations. If the inconsistency does not decrease, reduce StepSize for better reconstruction.

  • If you are using the gradient descent method, the L-BFGS optimizer usually provides the best results. This optimizer automatically selects the step size for each iteration. However, the L-BFGS optimizer may require more computation time than other optimizers to run the same number of iterations.

References

[1] Griffin, Daniel W., and Jae S. Lim. "Signal Estimation from Modified Short-Time Fourier Transform." IEEE Transactions on Acoustics, Speech, and Signal Processing. Vol. 32, Number 2, April 1984, pp. 236–243. https://doi.org/10.1109/TASSP.1984.1164317.

[2] Perraudin, Nathanaël, Peter Balazs, and Peter L. Søndergaard. "A Fast Griffin-Lim Algorithm." In 2013 IEEE Workshop on Applications of Signal Processing to Audio and Acoustics, New Paltz, NY, October 20–23, 2013. https://doi.org/10.1109/WASPAA.2013.6701851.

[3] Le Roux, Jonathan, Hirokazu Kameoka, Nobutaka Ono, and Shigeki Sagayama. "Fast Signal Reconstruction from Magnitude STFT Spectrogram Based on Spectrogram Consistency." In Proceedings of the 13th International Conference on Digital Audio Effects (DAFx-10), Graz, Austria, September 6–10, 2010.

[4] Ji, Li, and Zhou Tie. “On Gradient Descent Algorithm for Generalized Phase Retrieval Problem.” In 2016 IEEE 13th International Conference on Signal Processing (ICSP), 320–25. Chengdu, China: IEEE, 2016. https://doi.org/10.1109/ICSP.2016.7877848.

Extended Capabilities

Version History

Introduced in R2020b

expand all