views:

56

answers:

1

Hello,

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.

+3  A: 

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);
Matt Mizumi
I tried implementing the function using wavread/wavrwite but it said the equation was unbalanced. I haven't got time to have another look at MATLAB right now but I appreciate your reply and i'll check it out later!
Velocity
@Matt Mizumi Thanks, I've managed to get the fade working on the first five seconds, but i'm not sure how to select the last 5 seconds of the clip. I assumed it would be something like fade_samples:end but I'm not sure it's correct?
Velocity
suggestion for fade out added; relevant MATLAB tutorial and help is here: http://www.mathworks.com/access/helpdesk/help/techdoc/math/f1-85462.html for reference.
Matt Mizumi
Thanks, that's fantastic. I have been researching arrays but I don't think I would have come up with that answer anytime soon. I've read the tutorial again though. Thanks!
Velocity