Memory leak from waveform plotting at high frequency

2 views (last 30 days)
Hi
I'm having problems with 16gb of RAM on my computer being taken up calculating the below.
I know its because of t and f, How do I code the sampling to be right at high frequencies such as 200-250 MHz so it plots at appropriate time intervals that is not taxing?
t = 0 : 0.01 : 1;
for f = 200000000 : 500000 : 250000000 % From 200 MHz to 245 MHz with 500 kHz increment
y(f,:) = sin (2 * pi .* f .* t);
end
figure(1)
plot(t,y)
figure(2)
%Also plot the sum of each waveform at each time interval t
plot(t, sum(y), ':r', 'LineWidth',2.5)

Accepted Answer

Star Strider
Star Strider on 5 Nov 2017
I don’t see the problem.
f = (200 : 0.5 : 245)*1E+6;
t = 0 : 0.01 : 1;
y = sin (2 * pi .* f(:) * t);
whos y
Name Size Bytes Class Attributes
y 91x101 73528 double
A 73.528 kB array isn’t that large.
  2 Comments
Nathan Kennedy
Nathan Kennedy on 5 Nov 2017
Edited: Nathan Kennedy on 5 Nov 2017
It does not plot, it's struggling with it and your code only plots one waveform?
And my original code doesn't seem to plot the frequencies properly, for example 2000 Hz clearly does not have 2000 oscillations on the plot of this code. It's to do with my sampling being off
Star Strider
Star Strider on 5 Nov 2017
My code plots all 101 waveforms (corresponding to ‘f’) in figure(1), and the mean of them (for each of the 101 points in ‘t’) in figure(2). Here, ‘y’ is a (101x101) double array. (I initially used the 245 MHz upper limit, producing the (91x101) array.)
The problem is that you are using ‘f’ as a loop index. That won’t work here, because it starts the array at 200000000, creating at the outset a vector of 200000000 8-byte double-precision numbers (all 0), or 1.6E+9 bytes. It then adds from there, eventually addressing an array of 2E+9 bytes.
To illustrate:
v(4) = 1
v =
0 0 0 1
Tweaking your original code a bit prevents this:
t = 0 : 0.01 : 1;
f = 200000000 : 500000 : 250000000
for k = 1:length(f) % From 200 MHz to 245 MHz with 500 kHz increment
y(k,:) = sin (2 * pi .* f(k) .* t);
end
Now both your code and mine produce the same size array:
Name Size Bytes Class Attributes
y 101x101 81608 double
My code (using simple vector multiplication to produce the ‘f*t’ argument matrix) runs about 4 times faster that the expressed loop. Other than that, they’re equivalent.

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 5 Nov 2017
t = 0 : 0.01 : 1;
num_t = length(t);
fvals = 200000000 : 500000 : 250000000;
num_f = length(fvals);
y = zeros(num_f, num_t);
for f_idx = 1 : num_f % From 200 MHz to 245 MHz with 500 kHz increment
f = fvals(f_idx);
y(f_idx,:) = sin (2 * pi .* f .* t);
end
figure(1)
plot(t, y)
figure(2)
%Also plot the sum of each waveform at each time interval t
plot(t, sum(y), ':r', 'LineWidth',2.5)
You just happen to have the same number of f values as you have time steps. MATLAB creates one line per column, and your different columns correspond to increasing frequency. You need to think about whether you instead want to plot(t, y.') to have it show increasing time for each line.

Nathan Kennedy
Nathan Kennedy on 5 Nov 2017
Edited: Nathan Kennedy on 5 Nov 2017
No idea what you mean by that. It crashes withn range above about 1Khz for example 1Ghz to 1.1Gz.
I just want to be able to plot any frequency range (within reason) such as 50 MHz bandwidth, sum them all at each time and then do a fft to Freq domain. I get the plot may look messy - but there's a specific reason all waveforms are needed on one plot even if it looks a mess.
Another problem is that frequencies are being folded up into nyquists without the proper values for t
  3 Comments
Nathan Kennedy
Nathan Kennedy on 5 Nov 2017
I think this is all solved now thank you. I sampled at twice my highest frequency. The FFT graph you gave me has frequencies outside the frequency bandwidth that is declared in the code which is odd right?
Star Strider
Star Strider on 5 Nov 2017
The frequency vector in my Fourier transform code is based on the time vector. If the time vector is not appropriate (my new time vector is appropriate) the frequency vector will be similarly affected.
I have to rely upon the information provided in your Question, and unless you state that there’s a problem with it, I assume it’s correct.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!