Shop OBEX P1 Docs P2 Docs Learn Events
Convolution — Parallax Forums

Convolution

geokonstgeokonst Posts: 48
edited 2007-12-05 22:14 in Propeller 1
Dear all. Out of curiosity; Has anyone attempted to use the propeller for convolution (or crosscorelation)? If yes, has anyone attempted to use it for convoluting analogue sources?
Thank you
«1

Comments

  • AleAle Posts: 2,363
    edited 2007-11-28 15:07
    If you can clearify what you mean by "convolute" and "deconvolute"...

    I can explain you what I know by convolution... :

    Imagine you have a protein, when you electrospray it it will be mutlple times charged with some gaussian amplitude distribution. In a low mass range mass spectrometer it will show as several peaks with different m/z and *ideally* a gaussian amplitude distribution. But that spectrum is not *real*. because ideally you should get one peak per substance, but as it is a protein it can be bind more ions, resulting in different ions with different m/z (from there more than one peak).

    Now, Mass_protein = any_m/z*charge

    That means that from two or more peaks you can deconvolute the signal and reconstruct the original peak mass.

    An example !

    Aprotinin has a mass of 6511.51 amu, but in a low mass rage spectrometer (z.B. TSQ 700 or TSQ7000), you will see three or four peaks at : 1085.25, 1302.3, 1627.87, 2170.5 m/z.

    Applying the formula above you get 5611.51, when the charges are 6, 5, 4 and 3.

    Has anything to do with this ? (If not, enlighten me please).
  • geokonstgeokonst Posts: 48
    edited 2007-11-28 15:20
    Ale: I do believe that what you described is a specialized form of convolution(and deconvolution).

    From wikipedia:
    In mathematics and, in particular, functional analysis, convolution is a mathematical operator which takes two functions f and g and produces a third function that, in a sense, represents the amount of overlap between f and a reversed and translated version of g.
    ·
    In the discrete domain what is usually done is first reverse one waveform in the time axis. Then slide one waveform in the time axis (delay by samples) and multiply them together.
    ·
    If the two functions are even remotely alike the convolution will give a spike at the sample delay that they were most alike.
    ·
    From there it’s up to your imagination what you can do. You can for example use the result and trigonometry to localise a sound source (what I am trying to do). Or use it for machine stereo vision or a 1000 more uses.
  • AleAle Posts: 2,363
    edited 2007-11-28 15:57
    It sounds intriguing. I haven't read something like that here, but I do not read everything... I'll dig up something smile.gif
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2007-11-28 16:52
    FWIW I think it should be convolve rather than convolute
  • RaymanRayman Posts: 14,221
    edited 2007-11-28 17:25
    I usually do this in the frequency domain (because it's easier)...
  • LeonLeon Posts: 7,620
    edited 2007-11-28 21:44
    Convolution is the basic operation used in FIR digital filters and some other DSP applications.

    Leon

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Amateur radio callsign: G1HSM
    Suzuki SV1000S motorcycle
  • geokonstgeokonst Posts: 48
    edited 2007-11-29 02:44
    Leon: true. So I could rephrase; has anyone attempted any of this kind of DSP with the propeller? (apart from Chip of course who made the stereospat). Propeller is much cheaper than the cheapest fpga.

    Rayman: do you usually do in the frequency domain using the prop or in general? Isn't swiping through all frequencies more computationally expensive? Please tell me more as I have never tryed using the freq domain.
  • RaymanRayman Posts: 14,221
    edited 2007-11-29 02:57
    All you need to do is implement fft in Spin or Assembly. You can find this simple algorith in Numerical Recipes... Convolution is just multiplication in frequency domain. So, fft your signals, multiply magnitudes, add phases, un-fft, and you have the result...
  • LeonLeon Posts: 7,620
    edited 2007-11-29 17:42
    Why not use an actual DSP chip. dsPICs are quite cheap and fairly easy to use.

    Leon

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Amateur radio callsign: G1HSM
    Suzuki SV1000S motorcycle
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-11-29 19:08
    Rayman said...
    All you need to do is implement fft in Spin or Assembly. You can find this simple algorith in Numerical Recipes... Convolution is just multiplication in frequency domain. So, fft your signals, multiply magnitudes, add phases, un-fft, and you have the result...
    Only problem is, there is no fft library released for the Propeller yet, and it's creation is not trival (and very difficult if the programmer does not have much experience programing or using the fft). If you have developed one, please submit it to the Object Exchange,·there have been repeated requests for a fft library.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • AleAle Posts: 2,363
    edited 2007-11-29 21:02
    Ok,

    Here is a 512 point integer fft, data is in HUB RAM, sin and cos are calculated with the tables (the only routine I know works). Disclamer: I did not test it. But it is writen

    I wrote it around july, I made a post about it. I hope it works. Put it to good use !

    Ale
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2007-11-29 21:03
    Anyone who does want to program up an FFT should take a look at numerical recipes in C (its available as a free download somewhere), I've used those in C a few times, you would just need to work out a nice assembly version. If I had a use for it I'd do it myself.

    Graham
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2007-11-29 21:04
  • RaymanRayman Posts: 14,221
    edited 2007-11-29 21:26
    The Pascal version of NR is probably closer to SPIN, I'd imagine...
  • RaymanRayman Posts: 14,221
    edited 2007-11-29 22:58
    Actually, now that I think about it... I've only done fft in floating point. You may want to look at doing it in integer or fixed point... I guess there's a SPIN floating point library too though..

    Paul: I would love to do the fft for the Prop... But, it's hard to justify for me now, because I'm not doing anything that needs it at the moment. I've coded fft and ifft in few different languages in the past and it wasn't that hard. But, now realize that the lack of native floating point might be an issue... I know there are integer versions of fft though, but maybe not in Numerical Recipes...
  • Greg PGreg P Posts: 58
    edited 2007-11-30 13:29
    Here's some Visual Basic 6.0 FFT code. To test that it was working properly I generated a number of sine wave with various amplitudes, frequencies, and random phase relationships in the time domain, took the FFT of the combined signal, then with the FFT magnitude/phase results reconstructed the original time signal by summing together sine waves of the proper magnitude, frequency,and phase IN THE TIME DOMAIN. I then overlayed the reconstructed time signal on top of the original time signal to check for errors. Perfect match every time! Perhaps someone can use this as a debugging tool for work with the Propeller: Create a time-domain data set with this code, serial transmit it to a buffer on the Propeller, do the FFT, ship the result back to the PC over the serial port to the VB application, then reconstruct the waveform in the time domain using the FFT transform values. If it matches we have good code.
  • geokonstgeokonst Posts: 48
    edited 2007-12-02 02:20
    So who still thinks convolution in the frequency domain is easier than the time domain (always for the prop)?

    I think time domain for small delays (as small as the mem allows) is much easier.
  • LawsonLawson Posts: 870
    edited 2007-12-02 05:17
    From my limited understanding of the subject of convolution, the choice of whether to do it in the time-domain or frequency-domain depends on the size of the samples being compared and the range of offsets looked at. Time domain convolution is fast for simple stuff but does not scale well. Frequency domain convolution has a larger overhead, but scales better.

    Marty

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Lunch cures all problems! have you had lunch?
  • geokonstgeokonst Posts: 48
    edited 2007-12-02 05:30
    I can only agree with you [noparse]:)[/noparse]
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2007-12-02 16:31
    Its often a case of the frequency domain just making more sense than the time domain or vice versa, for a filter it makes more sense to multiply the spectrum of the signal by the transfer function of the filter but for correlation then it makes sense to do a convolution in the time/space domain because you are likely to have the thing you are looking for in the data in that form.

    As convolution is nothing more than multiplying and shifting why not just code it up, it won't take you long and you have an audience of people that are happy to assist.

    Graham
  • LeonLeon Posts: 7,620
    edited 2007-12-02 17:52
    Isn't it multiplying and adding?

    Leon

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Amateur radio callsign: G1HSM
    Suzuki SV1000S motorcycle
  • ErNaErNa Posts: 1,751
    edited 2007-12-02 20:00
    I will try to explain convolution with simple words:

    We have to start with an excurse on vectors. A vector can be represented by a list of values, normally written: (1, 0 , 2, 3, 4, ...), where every single number is a component and the count of numbers N is the dimension of this vector. The length of the vector is the square root of the sum of the squares of all components. This length is called "scalar product". The square of one component can more generally be seen as the product of component times component. This little difference in point of view is very important. It gives you the ability to determine, if two different vectors are equal. Two vectors are equal, when they are of the same length AND if their scalar product equals this length.

    Whenever you compare to vectors to get a measure for their similarity, you have to follow these steps:
    1. Determine the length of the individual vectors and divide every component by this value. The result is a vector of length 1. This is called "normalisation".
    2. Determine the scalar product of these two vectors.

    If the result is 1, the vectors are equal. The original vectors at least show the same direction, the vector are colinear. The result can be less one, even zero.
    If the result is 0, the vectors are perpenticular and have no similarity.

    The closer the result comes to 1, the more similar the two vectors are.

    What has this to do with "convolution" and "correlation"?

    Let's start with correlation. This operation answers the question, if a given signal is hidden into another signal. Indeed, it is nothing but computing the scalar product.

    The most simple case is:

    two signals of same dimension:

    ( 1, 0, 1, 0, 1, 0, 1, 0, 0) , ( 1, 0, 1, 0, 1, 0, 1, 0, 0). Both signals are obviously identical, but can I get this result by "computation"?


    The length of the first vector: Sqrt ( 1*1 + 0*0 + 1*1 + 0*0 + 1*1 + 0*0 + 1*1 + 0*0 + 0*0 ) = 2

    The length of the second vector: Sqrt ( 1*1 + 0*0 + 1*1 + 0*0 + 1*1 + 0*0 + 1*1 + 0*0 + 0*0 ) = 2

    The scalar product of first and second vector: Sqrt ( 1*1 + 0*0 + 1*1 + 0*0 + 1*1 + 0*0 + 1*1 + 0*0 + 0*0 ) = 2

    -> They are equal!

    But what, if the two vectors are not of same dimension, the first one is longer?

    ( 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0)

    Now the scalar product is not defined! What to do?

    We have to compute the scalar product for the first 9 components and then we shift the shorter vector by one and recalculate and do this until we reach the end. For every shift we get a scalar product as an result and we can plot this as a function of shiftvalue. This function we call the correlation function.

    To correlate answers the question, if there is a signal hidden in another signal.


    Now let us analyse this situation:

    Signal 1: ( 1, 0, 1, 0, 1, 0, 1, 0, 0)

    Signal 2: ( 0, 1, 0, 1, 0, 1, 0, 1, 0)

    Obviously, these signals are perpenticular, for the scalar product is 0. But the second signal is made from the first by shifting one step. How to find this out by simple calculation?


    We just double the first vector:

    Signal 1: ( 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0)

    and then compute the correlation function. And we will see, there is one position with scalar product 1, other positions show a value between 0 and 1. According to the problem we are talking about we can say: a signal is hidden into another signal, if the scalarproduct exceeds a certain threshold value, may .9, maybe .9999. It depends.


    Convolution is nothing but the same play, seen from another point of view. If you have the correlated function as an input, you can ask the question: what was the original function? Backward computing is called "deconvolution". But, then you have to know the vector, which was used to compute the correlated function.


    How to solve all these problems depends on different conditions. Do you have computing power and memory? What is the timescale. Can you make a good first guess? And so on. FFT is for certain reasons used, but there is principle limit: FFT can only be computed for dimensions of 2power N. You can not deconvolute a signal with 112 values.
  • Fred HawkinsFred Hawkins Posts: 997
    edited 2007-12-02 21:35
    ErNa, nicely phrased.
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2007-12-02 22:19
    yes adding too but that's even easier [noparse]:)[/noparse]
  • geokonstgeokonst Posts: 48
    edited 2007-12-04 00:47
    ErNa, that was a great description of the logic behind convolution. Thank you, I couldn’t possibly do it better.


    Graham Stabler said...
    As convolution is nothing more than multiplying and shifting why not just code it up, it won't take you long and you have an audience of people that are happy to assist.
    Problem is I have very little experience coding in spin and since I need to do convolution between the inputs of two microphones, I wonder if I have to implement it in assembly for better timing (which is even worse for a newbie). This is why I asked if anyone had already tried to do convolution with the propeller. Coding something like this sounds trivial for an experienced developer but it’s huge for me.

    This is why I was (and still am) trying to feed the adc output to the stereo spatializer object which does the delay and then figure out a way to do the multiplication. The more I am trying this approach the more this seems like an overkill so I will probably try my best to write some convolution code instead during the next week(s).
  • ErNaErNa Posts: 1,751
    edited 2007-12-04 07:59
    Geo, I still can't imagine, what your goal is. Which information do you wish to extract from combining the signals of two microphones? I fear, there is no general mechanism, that can run in time (I don't mean "real time", but really "time") In general: convolution always destroyes information. The reason is: before applying the convolution process, you know the signals, after, you have a signal, that could be generated in different ways and you don't know, which one, you have to guess. Maybe, you are looking for a time shift of the signals? and determine the direction of the source?
  • geokonstgeokonst Posts: 48
    edited 2007-12-04 15:21
    I want to do a cross corelation of the signals so I can determine for what delay I have a peak and thus calculate the angle. The only information I need is the delay so I don't mind loosing the rest.
    ErNa said...
    I fear, there is no general mechanism, that can run in time (I don't mean "real time", but really "time")
    I am sorry but I do not understand what you are trying to say. The mechanism i have in mind is a DAQ running in one cog that stores a fixed nunber of stereo sound samples in a circular buffer (the size really depends on sampling frequency and required minimum delay (which depends in the distance between the microphones)). Then another cog taking different delays (different starting points within the buffer) and corelating the waveforms. Ideally it would swipe through all available delays but a few fixed delays could also be used in order to do this as fast as possible.

    I hope I was clear enough. Again; thanks for all the responses.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-12-04 17:51
    Something to note, convolution is a parallel-izable algorithm, It would be possible to divide it into subsections to be computed by multiple cogs for increased throughput. A good thing considering classic convolution is computational order O(n2), even the FFT with it's order of O(n log n) can have it's butterflys computed in parallel.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • ErNaErNa Posts: 1,751
    edited 2007-12-04 23:22
    You could do it the following way:
    First you create a short pulse and watch for the reflection. This gives you an impression, if there is an echo at all and what the rough runtime is. There might come different reflections from different reflectors, hopefully in different directions.
    Now you generate a periodical pulse train, the rate small enough to have the reflections separated.
    To lower the noise and increase precision, you then start the correlation, which is more an auto- then a crosscorrelation, for you receive twice the same signal, only timeshifted. The first estimation for the shift-value you know from the first pulse.
    The scalar product (multiply, add) reaches the highest value, if you timeshift is correct. Therefore you can calculate values for three shifts: one to short, one correct and a third to long, with same distance. If the peak is symmetrical, the short and long value will be equal. In the case, the correct one is really maximal. Test this presumption by slightly modifing the shift value. This is similar to a phase locked loop.

    After this works, you can proceed by creating a noise signal and correlate it, but I think, then a FFT is better to do the correlation.
  • geokonstgeokonst Posts: 48
    edited 2007-12-05 03:02
    ErNa, I believe that what you are describing is a sonar. In that sense what i want to make is a passive sonar. No pulses broadcasted. Just compare the phase difference (or delay) between two microphone (space separated) inputs - and calculate the angle of the source.

    Just note that I am not interested in sound impulses which is much easier. I am more interested in continuous sounds. Thanks for the replies.
Sign In or Register to comment.