gpt4 book ai didi

matlab - 如何通过 MATLAB 生成由不同频率滑音段组成的声音?

转载 作者:行者123 更新时间:2023-12-02 03:11:32 24 4
gpt4 key购买 nike

我想通过 MATLAB 生成一种声音,其中包含预定义数量的不同频率滑移(扫描)。为此我写了一段MATLAB代码。但是,我遇到了两个问题:

1) 当我播放声音时,声音在整个持续时间内都发出咔哒声。

-> 这可能与前一个扫描段结束时和下一个扫描段开始时的不同相位角有关。我试图解决这个问题(见下面我的代码) - 到目前为止没有成功。您将在此处看到这种声音的频谱图:

spectrogram of concatenated sound which is perceived with several 'soft' clicks

2) 当我发出声音时,除了这些更柔和的声音之外,有时还会有更明显的咔哒声。这在频谱图中清晰可见。--> 在这里我不确定问题可能是什么以及如何避免它。

spectrogram of concatenated sound with additional distinct click

我生成声音的代码如下:

clear all;
close all;

%% define stimulus parameters
soundDuration = 1200; % duration of sound
sf = 44100; % sampling rate
ampl = 0.05; % 0.05; % ampl
segmentDuration = 25; % duration of one standard segment in ms
nSegments = soundDuration/segmentDuration; % number of segments of which the sound should consist of
t = 0:1/sf:(0.025-1/sf); % time vector for segment

%% generate sound consisting of n sweep-segments
complexSound = [];
for iSeg = 1:nSegments

f1 = 1000:10:3000;
f1 = randsample(f1,1); % start freq in Hz for current sweep segment
f2 = 1500:10:4500;
f2 = randsample(f2,1); % end freq in Hz for current sweep segment

if iSeg == 1
sweep = ampl * chirp(t,f1,segmentDuration/1000,f2,'logarithmic'); % generate sweep-segment withou considering the phase

else
sweep = ampl * chirp(t+1/sf,f1,segmentDuration/1000,f2,'logarithmic',ph); % the current sweep starts with a t+1/sf later and with the phase angle with which the previous sweep ended

end
ph = -90+360*(f2*t(end)+1/sf); % calculate the phase at the time point at which the current sweep ends and from that calculate the starting phase for the next sweep

sweep = sweep';
complexSound = [complexSound; sweep]; % concatenate sweep segments to form the complex sound

end
stim = complexSound;
sound(stim,sf);

对于解决这些问题的任何帮助,我都很感激。

最佳答案

所以,基本上你想要的是调频。嗯,不是真的,但我们基本上可以滥用 matlab 为它提供的功能。诀窍是这样的:

  1. 我们创建一个包含与最终信号一样多的元素的向量。向量的每个元素都包含最终信号在该特定样本中应具有的频率。
  2. 我们使用 fmmod 函数创建一个 1Hz 信号,该信号由我们在 1 中创建的矢量调制。

我尝试调整您的代码并使其尽可能相似。但是,现在所有持续时间都以秒为单位。

请注意,我假设各个段之间的边缘没问题。因此,如果您有一段以 1500Hz 结束,而下一段以 2000Hz 开始,则这两者之间没有平滑的进展。也许 fmmod 函数可以稍微平滑一点,但我没有检查。

但是,这里是代码:

%% clear
clear all;
close all;

%% define stimulus parameters
soundDuration = 1.2; % duration of sound in seconds
sf = 44100; % sampling rate
ampl = 0.05; % 0.05; % ampl
segmentDuration = 0.025; % duration of one standard segment in s
nSegments = round(soundDuration/segmentDuration); % number of segments of which the sound should consist of
samples_per_segment = floor(sf * segmentDuration);

%% generate sound consisting of n sweep-segments
modulator = zeros(1, nSegments * samples_per_segment);
first_idx = 1;
for iSeg = 1:nSegments

f1 = 1000:10:3000;
f1 = randsample(f1,1); % start freq in Hz for current sweep segment
f2 = 1500:10:4500;
f2 = randsample(f2,1); % end freq in Hz for current sweep segment

modulator(first_idx:first_idx + samples_per_segment-1) = logspace(log10(f1), log10(f2), samples_per_segment); % we add the logarithmic progression from f1 to f2 here
first_idx = first_idx + samples_per_segment;

end
%% create final sound
stim = fmmod(modulator, 1, sf, 1); % here we abuse the frequency modulation function of matlab.
% we basically tell it to create a 1Hz
% sinewave and modulate it by the vector
% we created in the previous step. the
% last argument tells it basically that
% if i give it a 1, it should increase
% the frequency by 1Hz, if i give it a
% 100, it should increase the freq by
% 100Hz and so on...
stim = ampl .* stim; % multiply by the amplitude

%% plot...
spectrogram(stim,1000,[],[],sf,'yaxis'); % this provides a nice tf plot....

%% play
sound(stim,sf);

关于matlab - 如何通过 MATLAB 生成由不同频率滑音段组成的声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39644097/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com