sound of a sequence of 0 and 1

조회 수: 1 (최근 30일)
Mireia Boneta Camí
Mireia Boneta Camí 2020년 10월 23일
댓글: Rik 2020년 10월 24일
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
Mireia Boneta Camí
Mireia Boneta Camí 2020년 10월 23일
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 2020년 10월 24일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Freewing
Freewing 2020년 10월 23일
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개)

카테고리

Help CenterFile Exchange에서 Audio and Video Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by