Plotting a Surf of my waveform

3 views (last 30 days)
Hi, my waveform is
sampling = 1 / (1*10^6); t = 0: sampling :0.05 ; %Sample Rate
f = (30 : 1 : 35)* (10^3); %Frequency Range
for a = 1:length(f) -1
y(a,:) = cos (2*pi .* f(a) .* t)
end
plot (t, sum(y)) % my waveform of 5 frequencies combined
xlim ([0 3] *10^-3) ; ylim([-6 6])
I am trying to get a 3d plot of my waveform.
So I think it should be
surf(t, new_value, y)
How do I declare the new_value to get my 3dimensional shape and how to properly scale the grid to X limits in the 2d plot? Totally stumped and getting blank figures appearing Ive also tried the below to get the grid dimensions equal to my sampling
[x,y] = meshgrid(0:sampling:0.05)

Accepted Answer

Star Strider
Star Strider on 28 Nov 2017
I am not entirely certain what you want to do.
If I understand your Question, you can take advantage of vector multiplication to create a matrix of your frequencies and time, then just use mesh or surf to plot it. You do not even need meshgrid.
sampling = 1 / (1E+6);
t = 0: sampling :0.05 ; %Sample Rate
f = (30 : 1 : 35)* (1E+3); %Frequency Range
y = cos (2*pi .* f' * t);
figure(1)
mesh(t, f, y)
xlabel('Time')
ylabel('Frequency')
zlabel('Amplitude')
  6 Comments
Nathan Kennedy
Nathan Kennedy on 29 Nov 2017
Hi,
I got this working but the axis are not what I was expecting - they are the number of time slices and not the actually time. How could this be solved do you think?
[x,y] = meshgrid( start_t : sampling : end_t); %grid of sample time
z = zeros(length(x),length(y)) %for use in loop
for num_freq = start_freq : freq_spacing : end_freq-1
old_sum_z = z;
z = cos(2 * pi * num_freq *y );
new_sum_z = old_sum_z + z;
end
figure(1)
subplot(2,1,1)
plot(y,new_sum_z)
subplot(2,1,2)
surface = surf(new_sum_z,'edgecolor','none')
Star Strider
Star Strider on 29 Nov 2017
I can’t run your code because I don’t have the constants. However just looking at your code, I would plot it as:
plot((start_t : sampling : end_t),new_sum_z)
Initialise *‘z’*as:
z = zeros(size(y));
Since ‘new_sum_z’ is a vector, you cannot use it with the surface plot functions. Note that if you simply use repmat to create a matrix from it, the plot will duplicate the vector, and not change with respect to frequency.
Also, ‘x’ and ‘y’ will have the same results for length. Preallocating is good, however since you’re summing ‘z’ over time for each frequency in the loop, the result will always be a vector, not a matrix, and will always occupy the same memory locations. There is no reason to preallocate here.
I gave you the code that will plot the surface as functions of both time and frequency. I have no idea what you are doing.
Good luck!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!