sound of a sequence of 0 and 1

1 view (last 30 days)
Mireia Boneta Camí
Mireia Boneta Camí on 23 Oct 2020
Commented: Rik on 24 Oct 2020
Hello everybody,
I have this pulse_sequence and I'd like to make it sound with the sound function and cosinus I have below. The problem is that I want to make the ones as sound but the zeros as silence, and I don't know how to do it because the length of the pulse_seq is of 115 but the length of the cosine is of 48481. Perhaps using repmat would be feasible but I don't know how.
Thank you very much!
pulse_seq = "01010101000111011101110001011101010001011100000001011101110111011100010101110111011100010101011101110"
dot_duration = 0.06;
tone_frequency = 750;
sampling_frequency = 8000;
F=tone_frequency/sampling_frequency;
dot_samples=strlength(pulse_seq)*dot_duration
t_seq=0:1/sampling_frequency:dot_samples
sound(cos(2*pi*tone_frequency*t_seq),sampling_frequency)
  3 Comments
Mireia Boneta Camí
Mireia Boneta Camí on 23 Oct 2020
Hi Rik I don't understand what you mean by using a cell array to store my two sounds. What sounds? I'd like to make the pulse_seq sound as the cosine below, but only the ones (the 0s as silence)
Rik
Rik on 24 Oct 2020
I though you essentially wanted this:
pulse_seq = '01010101000111011101110001011101010001011100000001011101110111011100010101110111011100010101011101110';
for n=1:numel(pulse_seq)
if strcmp(pulse_seq,'0')
sound(zeros(size(t_seq)),sampling_frequency)
else
sound(cos(2*pi*tone_frequency*t_seq),sampling_frequency)
end
end

Sign in to comment.

Accepted Answer

Freewing
Freewing on 23 Oct 2020
First of all, you'd want your t_seq to be of length 48480 which is strlength(pulse_seq)*dot_duration*sampling_frequency:
pulse_samples = dot_duration * sampling_frequency;
num_samples = strlength(pulse_seq) * pulse_samples;
t_seq = (0:num_samples-1) / sampling_frequency;
Now you need a pattern of 0s and 1s of length num_samples, you can make it in a loop like that:
sig = ones(1, pulse_samples);
pattern = zeros(1, num_samples);
pulse_char = char(pulse_seq); % convert pulse_seq to char array
for j = 1:length(pulse_char),
if pulse_char(j) == '1', % copy sig only when there's '1' in char array
ind = 1 + (j-1)*pulse_samples : j*pulse_samples; % index of pattern corresponding to j
pattern(ind) = sig;
end
end
Apply pattern to your cosine wave and play sound:
s = cos(2*pi*tone_frequency*t_seq) .* pattern;
sound(s, sampling_frequency);
Now if you want it to sound smoother (no clicks), you'd like to smooth out your sig pulse:
sig = ones(1, pulse_samples);
smooth_samples = 0.005 * sampling_frequency;
t = (0:smooth_samples-1) / (smooth_samples-1);
sig(1:smooth_samples) = sig(1:smooth_samples) .* smoothfunc(t);
sig(end-smooth_samples+1:end) = sig(end-smooth_samples+1:end) .* smoothfunc(1-t);
Use this function which goes from 0 to 1 smoothly as its argument goes from 0 to 1:
function s = smoothfunc(t)
s = -2*t.^3 + 3*t.^2;
end

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!