Skip to content

Instantly share code, notes, and snippets.

@jfsantos
Last active December 14, 2015 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfsantos/5091135 to your computer and use it in GitHub Desktop.
Save jfsantos/5091135 to your computer and use it in GitHub Desktop.
% shepardtones.m - plays the shepard tones
% based on an example I found here: http://hebb.mit.edu/courses/9.29/2003/athena/auditory/matlab/shepardtones.m
% adjustable parameters
max_time = 2; % time for each tone
max_repeat = 3; % number of loops
A = 440; % "base frequency"
num_harm = 7; % number of harmonics used for each tone
sigmasq = 1; % variance of the amplitude for each partial
fs = 8192; % the sampling rate
dt = 1/fs;
t = [0:dt:max_time];
k = -2:(-2+num_harm);
m = 2^(1/12); % semitones
% sigmasq = 1;
rep = [];
for j = 0:11
y = zeros(size(t)); % initialize the tone vector
for i = 1:num_harm % use each harmonic in k
f_ij = 2^k(i)*(A*(m^j)); % the frequency of this component
amp = exp((-1/2*sigmasq)*(log2(f_ij/A))^2);
y = y + amp*cos(2*pi*f_ij*t);
end;
rep = [rep y]; % append the finished tone to the tone vector
end;
rep = repmat(rep,1,max_repeat); % repeat max_repeat times
soundsc(rep,fs); % play it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment