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)

 Réponse acceptée

Star Strider
Star Strider le 28 Nov 2017

0 votes

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 commentaires

Nathan Kennedy
Nathan Kennedy le 28 Nov 2017
Modifié(e) : Nathan Kennedy le 28 Nov 2017
Hi
I attached what my plots look like. Its the second one that I would like 3Dimensional. Its the sum from my code I wrote in first post. plot(t,sum(y))
if that's clearer? I didn't post all the code to get my plots just the part that creates subplot number 2
Star Strider
Star Strider le 28 Nov 2017
My code does what you want. Did you run it?
It produces a 3D plot of the waveform as a function of frequency and time. Because of the frequency and time vector values, it looks essentially solid, without any obvious periodicity. If you lower the frequencies, the periodicity is obvious.
Nathan Kennedy
Nathan Kennedy le 28 Nov 2017
Modifié(e) : Nathan Kennedy le 28 Nov 2017
Yes I have plotted but I am struggling to decipher how to edit it.
I was under the assumption to plot the second plot as a 3D, then I would need a cosine function to go in and out of the screen so it creates 3d image.
So I was thinking if the time = x. My function that's already written and called sum(y) is actually the z axis (up-down or North-South) then the y axis (going in and out of the page) actually should be the sum(y) too? Thus creating a Matrix x:y:z which for my code is t:sum(y):sum(y) ?
If you temporarily define:
f = (30 : 1 : 35);
You will get an idea of the underlying plot. You can go from there to create it as you want it.
You cannot use ‘sum(y)’ in the mesh plot because it reduces ‘y’ to a vector, and you cannot have vectors as the ‘z’ argument in surface plots. They must be matrices.
If you want to plot the summation, a 2D plot is your only option:
figure(2)
plot(t, sum(y))
grid
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')
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!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Performance dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by