# Duration of generated sound not correct

조회 수: 4 (최근 30일)
Anton M 2022년 3월 25일
답변: Mathieu NOE 2022년 3월 25일
Hello,
I need to create 25ms ramped sounds with different frequencies. I was successful in creating the ramp, but something is not right with the duration as it is always longer than what is inserted. I think it is related to the sampling rate but I cant figure out the exact problem:
Amp = 0.5; %amplitude
Freq = 1000; %Frequency of the sound
Fsam = 44100; %Sampling rate
Dur = 2; %Duration of the sound in seconds
dt = 1/Fsam
Time = 0: dt :Dur-dt;
y = Amp*sin(2*pi*Freq*Time);
plot (y);
xlabel('Time (ms)');
ylabel('Amplitude');
%% Ramp the sound
y = Amp*sin(2*pi*Freq*Time);
RampDur = 0.01;
NsamRamp = round(RampDur/dt);
OnRamp = linspace(0,1,NsamRamp);
OffRamp = fliplr(OnRamp);
y(1:NsamRamp) = OnRamp.*y(1:NsamRamp);
y(end-NsamRamp+1:end) = OffRamp.*y(end-NsamRamp+1:end);
plot(Time, y);
xlim ([0 Dur]);
sound(y)

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

### 채택된 답변

Scott MacKenzie 2022년 3월 25일
편집: Scott MacKenzie 2022년 3월 25일
Change
sound(y);
to
sound(y,Fsam);
The sound function assumes a sampling rate of 8192 Hz. If a different sampling rate is used, it must be provided as a 2nd argument.
##### 댓글 수: 2없음 표시없음 숨기기
Anton M 2022년 3월 25일
Thank you Scott this works! And it also fixed the frequency which was my second question as the 1kHz sound did not sound like a 1kHz. Awesome!
Scott MacKenzie 2022년 3월 25일
@Anton M, you're welcome. Good luck.

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

### 추가 답변 (1개)

Mathieu NOE 2022년 3월 25일
hello
some minor modifications and suggestion
Amp = 0.5; %amplitude
Freq = 1000; %Frequency of the sound
Fsam = 44100; %Sampling rate
Dur = 2; %Duration of the sound in seconds
dt = 1/Fsam;
% Time = 0: dt :Dur-dt; % no
Time = 0: dt :Dur; % yes
y = Amp*sin(2*pi*Freq*Time);
plot (y);
%xlabel('Time (ms)'); % no
xlabel('Time (s)'); % yes
ylabel('Amplitude');
%% Ramp the sound
y = Amp*sin(2*pi*Freq*Time);
RampDur = 0.01; % 25 ms ??
NsamRamp = round(RampDur/dt);
window = ones(size(y)); % init window to 1
window(1:NsamRamp) = linspace(0,1,NsamRamp); % ramp up section
window(end-NsamRamp+1:end) = linspace(1,0,NsamRamp); % ramp down section
y = window.*y;
plot(Time, y);
% xlim ([0 Dur]); % not needed
still I don't understand where the 25ms should apply (the ramp duration ? so why define RampDur = 0.01 ? )
the code does generated a 2 seconds long signal as you requested - there is no big surprise here ; what is your issue ?

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

### 카테고리

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