MATLAB Why does this Matlab snippet create biased Fourier waveforms?

AI Thread Summary
The Matlab snippet initially produced biased Fourier waveforms due to an unnoticed multiplication factor affecting the parameters. The user was exploring a range of waveforms defined by 20 parameters, including Fourier coefficients and frequency/phase parameters, all generated randomly. Despite the uniform distribution, the waveforms were consistently greater than zero, leading to concerns about bias. Upon reevaluation, the user identified the source of the bias as an error in the code, which has since been corrected. The issue highlights the importance of thorough code review in programming.
Andrew732
Messages
9
Reaction score
0
Why does this Matlab snippet create "biased" Fourier waveforms?

I'm trying to search through the space of all possible waveforms (or a reasonable approximation of that space), where waveforms are described by 20 parameters: the first 9 Fourier coefficients for sine terms a(1:9), the first 9 Fourier coefficients for cosine terms b(1:9), a frequency parameter w, and a phase parameter p. The range of the parameters are uniform random over the range [-0.2, 0.2] for each element in a and b, [-1, 1] for w, and [-2 * pi, 2 * pi] for p. The Matlab code that creates a waveform using the parameters is simply:

Code:
t = 0:0.1:10;
wave = zeros(1, numel(t));
for count = 1:9
s = a(count) * sin(count1 * w * t + p);
s = s + b(count) * cos(count1 * w * t + p);
wave = wave + s;
end

This makes waveforms that are fine for my purposes except for a bias that I have noticed over thousands of runs and that I can't explain. Given the uniform random values for the parameters, why would the values of the waveform be greater than 0 significantly more often than 50% of the time? This is not a homework question, just something that's come up in a program I'm writing. I'm mostly just curious and am probably doing something dumb. Thanks for any help!
 
Physics news on Phys.org


Well assuming that you're using the rand() function you should have like a 67% change of being within one standard deviation of 0.

What is the code that you are using to generate your interval of [-2*pi,+2*pi]?
 
Last edited:


Actually, phrasing the question forced me to take another look at my code and I discovered that I was multiplying everything by another factor, unlike in the code I actually posted, so there's no bias anymore. Thanks anyways!
 

Similar threads

Replies
3
Views
3K
Replies
2
Views
2K
Replies
2
Views
4K
Replies
1
Views
3K
Replies
6
Views
5K
Back
Top