MatLab

Discussion in 'Programmer's Corner' started by Yeti, Sep 8, 2005.

  1. Yeti

    Thread Starter Active Member

    Jul 26, 2005
    35
    0
    I have jsut recently started to use MatLab for a class i am taking, and We are playing with .wav files and such. I was wondering if in MatLab i can modify a recording of my voice into MatLab and make the output sound different like darth Vader or some other "famous" voice?
    Thanks
     
  2. Brandon

    Senior Member

    Dec 14, 2004
    306
    0
    Heh.. much much much more my friend.

    MatLab is a DSP programmers little playground. There are tons of filters and other algorithems and you can build your own. Reverbs, delay, overdrive etc.

    For a class projects I wrote 2 programs.

    #1 Program read in a 10 sec .wav, chopped it up. Doing some DSP the program would figure out what was a 'sh' and 'th' sound or what were vowel sounds and would sample them in different ways, it would them predict a signal which was similar to the little snippet of voice and used this to contruct the same line but sounding like Wargames. Then we took the chunks (phonemes) and would generate our own words. Was neat.

    #2 Program would take a text file, turn it into a .wav file with an am modulated carrier of the text file as a binary sequence, basically a square wave. It would then take that signal and superimpose it over say another wav from a cd with little distortion, you heard a little noise some times. But then you would use the program and take the combined .wav, and filter out the music, demodulate the carrier and you have the text file in hand. Great way to give a message right over the radio where noise really isnt an issue.
     
  3. Yeti

    Thread Starter Active Member

    Jul 26, 2005
    35
    0
    So what would the code/script look like to make me sound like darth vader if i have a 10 second wave file of my voice?


    [y,fs,nibts]=wavread('voice.wav');
    .......
    .....some manipulations
    ...
    sound(y,fs); %Darth vader voice :)

    Can someone fill in the blanks(dots in this case) for me?
    Thanks
    Yeti
     
  4. Brandon

    Senior Member

    Dec 14, 2004
    306
    0
    Yeti.. its not that simple unless you can find something like it online and download a copy of it. Its not just something as basic as a few functions. MatLab has the DSP building blocks, you need to figure out what you want it to sound like and design up an algorithm for it.

    If you're not really that experienced in DSP or not really sure what reverb, delay, overdrive, phasors, flangers, etc do or anything about sampling, you're going to need to do a little bit of homework before you get started if you want to understand whats going on.

    A couple of functions your definately going to need are

    lpc
    butter
    cheby
    conv
    filter
    resample
    interp or interpolate, i forget

    check the helps on those, but some of them will require you to understand some basic signal processing otherwise its going to be greek.

    Online I'm sure you can find algorithms for reverbs, delays and other effects as in they are only mathematical sequences in the end. It will look something like this
    x[t]=input
    y[t]=output

    y[t]=x[t] + A * x[t-Delay] + A/2 * x[t-2*Delay] + A/4 * x[t-3*Delay]

    Basically the output at anytime t of your input signal of x[t] will be the input signal and 3 echoes each at lower intensities as the sound echoes out spaced Delay samples apart.

    y[t]=x[t/2]

    The output is the input, but the time is now stretched by 2x.
    y[0]=x[0]
    y[1]=x[1/2]=x[1]
    y[2]=x[2/2]=x[1]
    etc..

    This drops the sound by an octive making your voice deeper and it also slows down play back as well, you can then resample it back to proper speed but keep the deepend voice.

    Hope this helps you get started.
     
  5. Yeti

    Thread Starter Active Member

    Jul 26, 2005
    35
    0
    "Online I'm sure you can find algorithms for reverbs, delays and other effects as in they are only mathematical sequences in the end. It will look something like this
    x[t]=input
    y[t]=output

    y[t]=x[t] + A * x[t-Delay] + A/2 * x[t-2*Delay] + A/4 * x[t-3*Delay]"

    So this basically adds the 'talking into a fan' effect right? like from the movie TOmmy Boy.

    So once I get the script figured out(coding, not sure what its called for matlab) how can you convert the mathematical functions into analog circuit components?

    Thanks for your help,
    Yeti
     
  6. Brandon

    Senior Member

    Dec 14, 2004
    306
    0
    Not exactly. Talking into a fan would be akin to chopping out a sample here and there at a predetermined interval to give the sound a stuttering effect.

    What I wrote would be more like an echo which fades out with each echo Delay samples apart.

    Not without lots and lots of time and a very good understanding of DSP and Linear Algebra/Systems.

    It would be easier in the end to just build flangers, phasors, reverb, delays, etc because you can find these very easily online. It was a paper back book about 400 pages I had years ago which contained tons of make it yourself audio effects. Wish I could remember the name of it because you'd love it.

    If your final desire is to make this out of analog components, don't waste time going the DSP route in MatLab. Just build the components and start playing.

    On the otherhand, you really don't need to go back to analog. If you have a fast enough microprocessor or 2, you can use the mathematical expressions for the filters/effects inside of the processor and have it process the effects in real time or with a slight time delay depending on how well you program the equasions, if you pipeline them, window sizes, etc.

    Since MatLab is built on C, there is a C compiler within MatLab you can use to turn a MatLab script into a compiled C code, and if you have a microprocessor you can program in C, you've already handled the interfacing.
     
  7. Yeti

    Thread Starter Active Member

    Jul 26, 2005
    35
    0
    Ok well I have start to the file. I can get the reverb(had to do it for class :) and the dropping of the octave in voice. I dont think i understand the help file for resample to use it as you suggested. here is a copy of what I have so far. Any suggestions would be appreciated. ALso the octave dropping loop takes a while to complete, i think its around 20 seconds or so. Is there anyway to speed this up?
    Thanks
    Yeti


    clear all;close all; clc;
    [y,fs,nbits]=wavread('wavone'); %READ FILE IN
    t=[1:length(y)]/fs;t=t:)); %SET t UP AND MAKE IT A COLUMB VECTOR

    x=0*y;
    wavplay(y);
    for b=[1:length(y)]
    x(2*B)=y(B); %This drops the octave on the voice.
    end
    T=.147; %DELAY TIME
    for n=0:4 %LOOP TO PLAY 3ECHOES AT LOWER AMPLITUDES AFTER EACH sound(x*.5^n,fs);pause(T);
    end
     
  8. Brandon

    Senior Member

    Dec 14, 2004
    306
    0
    MatLab for all its greatness manages memory like crap. You need to init all your array, matrices, etc BEFORE you assign values to them. just do a zeros(m,n) for m x n matrix or just zeros(m) for a m column 1 row matrix.

    Additional, you don't need to use a loop many times in MatLab. The code can handle this by itself by preforming vector or matrix calculations more quickly.

    repost the code w/o smilies activated so I can see if we can skip the for loop for the lower octive. From what I can see its going to do some skipping unintentiually since you did 2*b and not b/2.

    try this out

    x=zeros(length(y)*2);
    newx=zeros(length(y));
    for b=[1:2*length(y)],
    x(b)=y(ceil(b/2));
    end
    newx = resample(x,1,2);
    sound(newx,fs);

    OR

    x=zeros(length(y)*2);
    newx=zeros(length(y));
    for b=[1:length(y)],
    x(2*b-1)=y(b);
    x(2*b)=y(b);
    end
    newx = resample(x,1,2);
    sound(newx,fs);

    One of them will be faster, not sure which.

    reample
    y = resample(x,p,q) resamples the sequence in vector x at p/q times the original sampling rate, using a polyphase filter implementation. The length of y is equal to ceil(length(x)*p/q). p and q must be positive integers. If x is a matrix, resample works down the columns of x.

    x=[1 2 3 4]
    i.e. y=resample(x,2,1) (2/1 = 2 so we double the samples)
    y=[1 1.5 2 2.5 3 3.5 4 4.5] or similar

    x=[1 2 3 4 5 6 7 8 9 10]
    y=resample(x,1,2) (1/2 = .5 so we loose 1/2 the samples)
    y=[1 3 5 7 9] or similar

    resample is akin to what encoding into an MP3 does. Resamples as a slower rate so you use less memory for the same data nearly.

    Now for you echo, you're using code in MatLab and not doing it the way you should. Rather than space out the play backs do something liket this.

    Y Final Output
    X Input Signal
    A1,A2,A3 Amplitude Levels for echos
    T = Delay Time
    P = Delay Period = round(1/T)
    Z(#P) = zeros(P*#)

    X1=[Z(1P) A1*X]
    X2=[Z(2P) A2*X]
    X3=[Z(3P) A3*X]
    Y=X + X1 + X2 + X3

    --> sound(Y,fs) now this will play all the echos by itself.

    the X1, X2 crap is inserting silence (the zeros) before the sound file of X so it is delaying the signal from playing. When you add them all together you will get the overlap you hear in many songs when people sing. I believe you might get an vector length error if you try this as is. I belive you need to zero pad everything at the end to get al the vectors to be the same length.

    REMEMBER, any vector or matrix you intend to use must be initialized before hand otherwise you will continue to get the long delays.
     
Loading...