Creating a fade-in / fade-out function in MATLAB?

I am looking to create a function that could create a fade-in / fade-out function in a WAV file in five seconds.

I found this code on the MATLAB forums, but it seems that the implementation was a bit wrong, although there is a good idea. This was for 300ms .WAV files with 10 second attenuation / output:

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 see what he was trying to do, trying to scale the first 10 waveform samples read by the incremental function using linspace, but I tried changing and changing it, but I can't get it to work.

Does anyone have any suggestions? Thank.

+3
source share
1 answer

, , , - :

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)

, linspace - , , , ...

: ,

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

Source: https://habr.com/ru/post/1760271/


All Articles