06 December 2006

matlabsession ronny and tony

Today we try to implement some effects. Some notes:
  • Instead of wavplay() you can also use sound(vector,sampleFrequency), it does fairly the same. Also stereo if the matrix is Nx2. soundsc() even plays the sound at the maximum amplitude without clipping.
    With wavrecord() one can record wave audio from the windows wave input device.
  • When using wavread and wavplay and others, pay attention to play at the same sample frequency as read. Otherwise the sound will mostly play as if it passed through an lowpass filter, because the standard Fs = 11025Hz
  • Ctrl+C is used to terminate a calculation.
  • switch/case statement can be used in matlab too
  • m-files can make use of cells to make the overview clearer
I tried implementing a flanger effect I found on the mathworks exchange server. Herefore I made a function that I can use throughout our quest for cool effects:
function[] = applyEffect(effectFile,soundFile)
Example: applyEffect('flange','x1.wav');
<< soundfile =" 'redwheel.wav';" effectfile =" '';" variation =" .01;" rate =" .3;" outputsignal =" flange(inputSignal," outputsignal =" signal;">>
This works well. I tried it with flange and after experimenting with the parameters variation and rate it worked nice enough.
Now I'm trying to understand every line of flange.m for future use... Some notes:
  • : Colon.
    J:K is the same as [J, J+1, ..., K]
  • CEIL(X) rounds the elements of X to the nearest integers
    towards infinity.
  • ROUND(X) rounds the elements of X to the nearest integers.
  • and thus: CEIL(X) <= ROUND(X) CEIL(X) >= ROUND(X) >= FLOOR(X)
  • >> abs([1 2 3;4 5 6;7 8 -9])
    ans = 1 2 3
    4 5 6
    7 8 9
  • .* Array multiply.
    X.*Y denotes element-by-element multiplication. X and Y
    must have the same dimensions unless one is a scalar.
I almost succeeded understanding the whole m-file about flange. Got stuck with this line:
signal = signal(cosineSamples+modulation-lfo);
signal is a column vector and between the brackets there is a row vector, what could this mean then?

I include the flange.m file in the version of today:
function [signal]=flange(signal, sampleFrequency, variation, rate)
%[y] = flange(fs, v, x, r)
%
% This is a basic flanging effect.
%
% fs = Sample rate
% v = Variation.
% x = Input audio signal. This should be a column
% vector.
% r = Rate.
%
% Example:
%
% >>y = flange(fs,0.002,x,0.5);
%
%
% See also WAVREAD and SOUND
%
%Version 1.0
%Coded by: Stephen G. McGovern, date: 08.03.03

modulation = ceil(variation*sampleFrequency);
%a row containing a raising value per sample of the signal
cosineSamples = 1:length(signal)+ modulation;
variation = round(variation*sampleFrequency);
padding = zeros(modulation,1);
%maxSample is the biggest amplitude of the vector or matrix signal
maxSample = max(abs(signal))
signal = [padding;signal;padding];
%period in seconds
period = 2*pi/round(sampleFrequency*rate);
%approximates a cosine which variates about variation/2 as an LFO
lfo = round((variation/2)*(1-cos(period.*cosineSamples)));
%chooses the samples indicated by the vector
cosineSamples+modulation-lfo, from signal
signal = signal(cosineSamples+modulation-lfo);
maxSample = maxSample/max(abs(signal));
signal = maxSample*signal;

2 comments:

toon said...

there is one mistake in your post, namely: CEIL(X) <= ROUND(X). the ceil command rounds a real number to the nearest higher integer, so
CEIL(X) >= ROUND(X). also note that the opposite effect can be obtained with the floor function, and as a consequence one can say that CEIL(X) >= ROUND(X) >= FLOOR(X).

toon said...

if cosineSamples+modulation-lfo is a vector, then the command
signal = signal(cosineSamples+modulation-lfo);
means that you select from the vector signal those elements whose indices appear in cosineSamples+modulation-lfo. this only makes sense if the elements of cosineSamples+modulation-lfo are non-negative integers.

an example:
cosineSamples+modulation-lfo = [1 3 4];
signal = [0:0.2:1]';
signal(cosineSamples+modulation-lfo) is then a vector containing the first, third, and fourth element of the original signal vector, i.e.,

signal(cosineSamples+modulation-lfo) = [0;0.4;0.6]