error in plotting clipped sinewave
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Osama Aliwat
el 19 de Dic. de 2020
Editada: Les Beckham
el 20 de Dic. de 2020
K=1;
UpD=input('Enter value of alpha in radian =');
DwD=input('Enter value of gamma in radian =');
for t=0:0.000001:2*pi
if t<UpD
y=0;
elseif pi<t<DwD
y=0;
else
y=K*sin(t)
end
end
plot(t,y)
grid
title('Distorted signal wave')
xlabel('Phase angle in degree')
ylabel('y(wo)')
I am trying to plot clipped sinewave and I expect my output to be like in the figure below but instead I am getting blank
Respuesta aceptada
Les Beckham
el 19 de Dic. de 2020
Editada: Les Beckham
el 19 de Dic. de 2020
Your problem is that y is not a vector. In the case of your posted code, y will be the value that it is set to on the last pass through your for loop (sin(2*pi) or a nearly zero number, due to floating point precision). So, the plot will be a about 6 million points at nearly zero which you won't be able to see (plot will expand your scalar y to match the dimensions of t -- not that I agree with that feature, but it is true).
So here are a couple of things you should consider:
- If you wish to use a loop (which is OK for learning), add an index to the calculation of y. To do this, you should define your time vector before the loop and then loop over the elements of t and wherever you assign y, assign a new element of the y array based on the currently indexed value of the t array, using the loop index. Note that, for efficiency, you should pre-allocate your y array before the loop (make a space in memory for it so that Matlab doesn't have to keep finding a new space in memory for it as it grows during the loop). For example:
t = 0:0.01:2*pi;
y = zeros(size(t));
for idx = 1:numel(t)
if t(idx) < UpD
y(idx) = 0;
...
end
- Do you really believe that your monitor is going to be able to display over 6 million points along your x axis? If not, reduce the step size of your t vector.
Note that there are more efficient ways to do this, without a loop, using logical indexing, for example. Search the Matlab help for that when you want to learn more.
2 comentarios
Osama Aliwat
el 20 de Dic. de 2020
Editada: Osama Aliwat
el 20 de Dic. de 2020
Les Beckham
el 20 de Dic. de 2020
Editada: Les Beckham
el 20 de Dic. de 2020
With your updated code I don't get a straight line. It isn't quite right yet, though.
Your elseif condition needs to be fixed. You can't do multiple conditions like you would on paper. For example 2<x<3 to test for x between 2 and 3 doesn't work in Matlab. You have to write this as two separate condtions anded together. So you would write this condtion as 2<x && x<3 (for a scalar x). This is also why your last set of logical indexing attempts didn't work.
I think this should help you fix your code.
Also, if DwD is supposed to be the size of the flat spot starting at pi, you might want to test against (pi+DwD).
Más respuestas (0)
Ver también
Categorías
Más información sobre Title en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!