Memory leak from waveform plotting at high frequency
2 次查看(过去 30 天)
显示 更早的评论
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)
0 个评论
采纳的回答
Star Strider
2017-11-5
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 个评论
Star Strider
2017-11-5
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.
更多回答(2 个)
Walter Roberson
2017-11-5
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.
0 个评论
Nathan Kennedy
2017-11-5
编辑:Nathan Kennedy
2017-11-5
3 个评论
Star Strider
2017-11-5
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!