在 MATLAB 中创建淡入/淡出函数?

发布于 2024-09-15 01:26:40 字数 623 浏览 9 评论 0原文

我希望创建一个函数,可以在 .wav 文件上创建五秒内的淡入/淡出功能。

我在 MATLAB 论坛上找到了这段代码,但似乎实现略有错误,尽管正确的想法是存在的。它适用于 300 毫秒的 .WAV 文件,淡入/淡出时间为 10 毫秒:

tenmssamples = length(soundfile)*10/300;
fade1 = linspace(0,1,tenmssamples);
fadedsound = soundfile .* ...
  [fade1, ones(1,length(soundfile)-2*tenmssamples), fliplr(fade1)];


tenmssamples = length(soundfile)*10/300;
fade2 = sin(linspace(0,2*pi/4,tenmssamples));
fadedsound2 = soundfile .* ...
  [fade2, ones(1,length(soundfile)-2*tenmssamples), fliplr(fade2)];

我可以通过尝试使用 linspace 缩放由递增函数读取的波形的前 10 个样本来了解他试图做什么,但我尝试过修补并修改它,但我无法让它工作。

请问有人有什么建议吗?谢谢。

I am looking to create a function that could create a fade-in/fade-out function on a .wav file over a period of five seconds.

I found this code on the MATLAB forums but it seems the implementation was slightly wrong, although the right idea is there. It was for .WAV files of 300ms with a 10ms fade-in/out:

tenmssamples = length(soundfile)*10/300;
fade1 = linspace(0,1,tenmssamples);
fadedsound = soundfile .* ...
  [fade1, ones(1,length(soundfile)-2*tenmssamples), fliplr(fade1)];


tenmssamples = length(soundfile)*10/300;
fade2 = sin(linspace(0,2*pi/4,tenmssamples));
fadedsound2 = soundfile .* ...
  [fade2, ones(1,length(soundfile)-2*tenmssamples), fliplr(fade2)];

I can see what he was trying to do by trying to scale the first 10 samples of the waveform read by an increasing function using linspace, but I have tried to tinker and modify it but I cannot get it to work.

Does anyone have any suggestions please? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戈亓 2024-09-22 01:26:40

我不确定你遇到的问题是什么,但我会做这样的事情:

Fs = 1000; % sampling rate of signal
FADE_LEN = 5; % 5 second fade

sig = randn(15.*Fs,1); % generate 15 s signal

fade_samples = round(FADE_LEN.*Fs); % figure out how many samples fade is over
fade_scale = linspace(0,1,fade_samples)'; % create fade

sig_faded = sig;
sig_faded(1:fade_samples) = sig(1:fade_samples).*fade_scale; % apply fade

subplot(211)
plot(sig)
subplot(212)
plot(sig_faded)

当然你可以用 sigmoid 等其他东西替换 linspace,并使用相同的想法进行淡出...

编辑:要淡出,请尝试

sig_faded(end-fade_samples+1:end) = sig(end-fade_samples+1:end).*fade_scale(end:-1:1);

I'm not sure what the problem you are encountering is, but I would do something like this:

Fs = 1000; % sampling rate of signal
FADE_LEN = 5; % 5 second fade

sig = randn(15.*Fs,1); % generate 15 s signal

fade_samples = round(FADE_LEN.*Fs); % figure out how many samples fade is over
fade_scale = linspace(0,1,fade_samples)'; % create fade

sig_faded = sig;
sig_faded(1:fade_samples) = sig(1:fade_samples).*fade_scale; % apply fade

subplot(211)
plot(sig)
subplot(212)
plot(sig_faded)

of course you can replace the linspace by something else like a sigmoid, and use the same idea to do a fade out...

EDIT: to do the fade out, try

sig_faded(end-fade_samples+1:end) = sig(end-fade_samples+1:end).*fade_scale(end:-1:1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文