Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating a random vector in MATLAB

I'm trying to generate a random frequency spectrum and then use ifft to find the corresponding time domain vector.

I'm using the following code:

for i = 1:64
    randNum = (rand() + 1i * rand())/sqrt(2); % Needs to be normalized by sqrt(2)
    randFreq(i) = randNum;
end

randVec = ifft(randFreq);

Note that I'm aware of mvnrnd, however for technical reasons I need to use a for loop and generate each element individually. I also need to generate the random frequency spectrum and then use an inverse transform, I can't directly generate a random vector in the time domain.

If I plot the magnitude of the random vector (plot(abs(randVec))), I always get a graph of this form.

enter image description here

There's always a spike at n=0 and all the other elements are significantly smaller in magnitude. I was hoping for some insight into why this was happening.

My question is not a duplicate of Create random values in vector Matlab, they are completely different questions that just happen to be on the same topic. I'm specifically asking about the behaviour of my ifft. It's not a duplicate of spike in my inverse fourier transform either. In that question, the spike could be caused by some idiosyncrasy of the data, however in my case the data is completely random.

like image 553
Farhad Avatar asked Jan 03 '18 22:01

Farhad


People also ask

How do you generate a random matrix in MATLAB?

The rand function generates arrays of random numbers whose elements are uniformly distributed in the interval ( 0 , 1 ). Y = rand(n) returns an n -by- n matrix of random entries. An error message appears if n is not a scalar. Y = rand(m,n) or Y = rand([m n]) returns an m -by- n matrix of random entries.

How do you generate a random number in a specific range in MATLAB?

Use the rand function to draw the values from a uniform distribution in the open interval, (50,100). a = 50; b = 100; r = (b-a). *rand(1000,1) + a; Verify the values in r are within the specified range.


1 Answers

The way you generate the random values for the frequency components, they are not symmetrically distributed about zero. Specifically, the obtained real and imaginary parts have an ideal average of .5/sqrt(2). The actual (sample) average will be close to that.

When you apply the IDFT, the average over all frequencies corresponds to the first sample in the time domain. To see this, set n = 0 in the IDFT expression:

                 enter image description here

So you get a larger absolute value at that first sample because the average over frequency is larger than it "should".

like image 95
Luis Mendo Avatar answered Oct 03 '22 15:10

Luis Mendo