MATLAB/Sound/Just and equal tuning

The two codes play the same riff, both using just and equal tuning. The only difference between the two versions is whether the just or equal tuned riff is first. If the variable testnote equals true, then major sixth above the first note is played at the end, for comparison.

On our speaker, it feels as if the sharper note is also louder. If that is true, then this is not a true test of whether the human ear prefers the just major sixth.

The codes can be copied and pasted into MATLAB either directly, or by going into Edit mode. The advantage of the latter is that if you select only the section, you can "select all" and then delete the first and last lines (<pre> ... <\pre>) after pasting.

justFirst.m

```clc; clear all; close all;
% Most sound cards support sample rates between approximately 5,000 and 48,000
% hertz. Specifying sample rates outside this range can produce unexpected results.
% User selected parameters:
testnote=true;
testnotes = [5/3, 2^(9/12)];
Repeats = 2; %Number of times the riff is repeated (in each tuning)
Duration=.3; %sets how long each note should be played (in seconds)
f0=221; % fundamental frequency
omega0=2*pi*f0; %Sets the fundamental frequency
fj=[1 3/2 5/3 3/2 5/3 3/2] % Bach prelude cello suite 1
bitf=.4; % This softens the tone burst.  .2 < bitf < 1.

% Most of these parameters never change:
dt=1/8192; %is Matlab's default sampling rate (period)
t = 0:dt:Duration; %sets the timeframe for a single note
notesInRiff = size(fj,2);
r = zeros(1,notesInRiff); %pre-assigns notes on 12-scale (logs)
% pitches for equal tuning begin with notes on the 12-tone scale
for notecount= 1:notesInRiff
r(notecount)=round(12*log2(fj(notecount)));
end %This rounds the just notes to logs
fs=2.^(r/12)
% begin comparison of riffs in the two tunings
for count = 1:Repeats
% just tuning begin
for n = 1:notesInRiff
omega=omega0*fj(n);
y=sin(omega*t);
% This section in required to soften the start and stop of the tone-burst
dfc=.5*size(y,2); %finds "center" of the array y
for j=1:size(y,2) % multiply y by a softened step function.
% this complicated function creates the soften step function
y(j)=y(j)*(   1  -   (   (j-dfc)/dfc   )^2    )^bitf;
end% section required to soften the start and stop of the tone burst.
soundsc(y);
pause(Duration)
end
% just tuning end
% equal tuning begin
for n = 1:notesInRiff
omega=omega0*fs(n);
y=sin(omega*t); % This section in required to soften the start and stop of the tone-burst
dfc=.5*size(y,2); %finds "center" of the array y
for j=1:size(y,2) % multiply y by a softened step function.
% this complicated function creates the soften step function
y(j)=y(j)*(   1  -   (   (j-dfc)/dfc   )^2    )^bitf;
end% section required to soften the start and stop of the tone burst.
soundsc(y);
pause(Duration);
end
%equal tuning end
end;
% This ends the code unless testnote = true
% Hear the difference after a short pause
if testnote==true
pause(2*Duration);
durd=5*Duration; % a different (longer) duration for the difference
td = 0:dt:durd; %new (larger) time array for difference tones

omega=omega0*testnotes(1);
y=sin(omega*td) ;
% This section in required to soften the start and stop of the tone-burst
dfc=.5*size(y,2); %finds "center" of the array y
for j=1:size(y,2) % multiply y by a softened step function.
% this complicated function creates the soften step function
y(j)=y(j)*(   1  -   (   (j-dfc)/dfc   )^2    )^bitf;
end
% ends section required to soften the start and stop of the tone burst
soundsc(y);
pause(1.1*durd);
omega=omega0*testnotes(2);
y=sin(omega*td) ;
% This section in required to soften the start and stop of the tone-burst
dfc=.5*size(y,2); %finds "center" of the array y
for j=1:size(y,2) % multiply y by a softened step function.
% this complicated function creates the soften step function
y(j)=y(j)*(   1  -   (   (j-dfc)/dfc   )^2    )^bitf;
end
soundsc(y);
end
```

equalFirst.m

```clc; clear all; close all;
% Most sound cards support sample rates between approximately 5,000 and 48,000
% hertz. Specifying sample rates outside this range can produce unexpected results.
%
% User selected parameters:
testnote=true;
testnotes = [5/3, 2^(9/12)];
Repeats = 2; %Number of times the riff is repeated (in each tuning)
Duration=.3; %sets how long each note should be played (in seconds)
f0=221; % fundamental frequency
omega0=2*pi*f0; %Sets the fundamental frequency
fj=[1 3/2 5/3 3/2 5/3 3/2] % Bach prelude cello suite 1
bitf=.4; % This softens the tone burst.  .2 < bitf < 1.
% Most of these parameters never change:
dt=1/8192; %is Matlab's default sampling rate (period)
t = 0:dt:Duration; %sets the timeframe for a single note
notesInRiff = size(fj,2);
r = zeros(1,notesInRiff); %pre-assigns notes on 12-scale (logs)
% pitches for equal tuning begin with notes on the 12-tone scale
for notecount= 1:notesInRiff
r(notecount)=round(12*log2(fj(notecount)));
end %This rounds the just notes to logs
fs=2.^(r/12)
% begin comparison of riffs in the two tunings
for count = 1:Repeats
% equal tuning begin
for n = 1:notesInRiff
omega=omega0*fs(n);
y=sin(omega*t); % This section in required to soften the start and stop of the tone-burst
dfc=.5*size(y,2); %finds "center" of the array y
for j=1:size(y,2) % multiply y by a softened step function.
% this complicated function creates the soften step function
y(j)=y(j)*(   1  -   (   (j-dfc)/dfc   )^2    )^bitf;
end% section required to soften the start and stop of the tone burst.
soundsc(y);
pause(Duration);
end
%equal tuning end
% just tuning begin
for n = 1:notesInRiff
omega=omega0*fj(n);
y=sin(omega*t);
% This section in required to soften the start and stop of the tone-burst
dfc=.5*size(y,2); %finds "center" of the array y
for j=1:size(y,2) % multiply y by a softened step function.
% this complicated function creates the soften step function
y(j)=y(j)*(   1  -   (   (j-dfc)/dfc   )^2    )^bitf;
end% section required to soften the start and stop of the tone burst.
soundsc(y);
pause(Duration)
end
% just tuning end
end;
% This ends the code unless testnote = true
% Hear the difference after a short pause
if testnote==true
pause(2*Duration);
durd=5*Duration; % a different (longer) duration for the difference
td = 0:dt:durd; %new (larger) time array for difference tones
omega=omega0*testnotes(2);
y=sin(omega*td) ;
% This section in required to soften the start and stop of the tone-burst
dfc=.5*size(y,2); %finds "center" of the array y
for j=1:size(y,2) % multiply y by a softened step function.
% this complicated function creates the soften step function
y(j)=y(j)*(   1  -   (   (j-dfc)/dfc   )^2    )^bitf;
end
% ends section required to soften the start and stop of the tone burst
soundsc(y);
pause(1.1*durd);
omega=omega0*testnotes(1);
y=sin(omega*td) ;
% This section in required to soften the start and stop of the tone-burst
dfc=.5*size(y,2); %finds "center" of the array y
for j=1:size(y,2) % multiply y by a softened step function.
% this complicated function creates the soften step function
y(j)=y(j)*(   1  -   (   (j-dfc)/dfc   )^2    )^bitf;
end
% ends section required to soften the start and stop of the tone burst.
soundsc(y);
end
```