Skip to content

Instantly share code, notes, and snippets.

@lostanlen
Created April 28, 2017 22:11
Show Gist options
  • Save lostanlen/ebf8ec5d67f1332cc9d916e825deb31c to your computer and use it in GitHub Desktop.
Save lostanlen/ebf8ec5d67f1332cc9d916e825deb31c to your computer and use it in GitHub Desktop.
Generate FM sine wave (vibrato) and reconstruct it from its scattering coefficients
% Add scattering library to path
addpath(genpath('~/scattering.m'));
% Parameters for vibrato signal
N = 8192;
sample_rate = 4096;
carrier_frequency = 1024;
modulation_frequency = 4;
bandwidth = 16;
% Parameter for scattering transform
Q = 16;
nOctaves = 2;
% Generate vibrato signal
t = (0:(N-1)).' / sample_rate;
phase = ...
carrier_frequency * t + ...
bandwidth * sin(2*pi*modulation_frequency*t) / modulation_frequency;
x = sin(2*pi*phase);
% Setup filter banks for scattering
clear opts;
opts{1}.time.size = N;
opts{1}.time.is_chunked = false;
opts{1}.time.nFilters_per_octave = Q;
opts{1}.time.gamma_bounds = [1 nOctaves*Q];
opts{1}.time.has_duals = true;
opts{1}.time.T = N;
opts{2}.time.nFilters_per_octave = 1;
opts{2}.time.has_duals = true;
opts{2}.time.T = N;
archs = sc_setup(opts);
% Compute scattering transform
[S, U] = sc_propagate(x, archs);
% Reconstruct
reconstruction_opts.is_verbose = true;
reconstruction_opts.nIterations = 50;
[iterations, relative_loss_chart, relative_layer_loss_chart] = ...
eca_synthesize_1chunk(x, archs, reconstruction_opts);
x_rec = iterations{end};
[S_rec, U_rec] = sc_propagate(x_rec, archs);
% Display scalograms of original vibrato and reconstruction
fig = figure(1);
%set(fig, 'WindowStyle', 'docked');
subplot(2, 1, 1);
display_scalogram(U{1+1});
title('Original scalogram');
subplot(2, 1, 2);
display_scalogram(U_rec{1+1});
title('Reconstructed scalogram');
colormap rev_magma
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment