sound of a sequence of 0 and 1
3 views (last 30 days)
Show older comments
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
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
Accepted Answer
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
0 Comments
More Answers (0)
See Also
Categories
Find more on Audio and Video Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!