How do I plot this sine wave?

119 visualizzazioni (ultimi 30 giorni)
Niamh Mc Devitt
Niamh Mc Devitt il 22 Ott 2013
Commentato: Image Analyst il 22 Ott 2013
I am new to matlab and I am struggling with the basics. I was asked this question in class and I'm just not sure what to do "Plot one second of a sine wave with frequency 97 Hz and phase shift 97 (in degrees). The x-axis should be time not samples". Please help.

Risposta accettata

sixwwwwww
sixwwwwww il 22 Ott 2013
Modificato: sixwwwwww il 22 Ott 2013
Dear Niamh, you can do the following way:
time = -1:0.01:1;
frequency = 97;
phase = 97;
phase_in_rad = degtorad(phase);
y = sin(2 * pi * frequency * time + phase_in_rad);
plot(time, y), xlabel('Time'), ylabel('Sine wave')
Do you need it or something else?
  2 Commenti
Image Analyst
Image Analyst il 22 Ott 2013
Modificato: Image Analyst il 22 Ott 2013
What you've actually inadvertently given is a good example of aliasing but you probably didn't even notice. However I don't think this is what the poster wanted. Do you know what aliasing is? How many periods to you see in 1 second? 97, or 3? Voila! Aliasing! With a step size of 0.01, how many samples will you get in 1/97 of a second? Only one! One sample per period, actually a little bit less so that's why you see only 3 periods instead of 97. Now, how many samples do you require to avoid aliasing? Check out http://en.wikipedia.org/wiki/Nyquist_frequency
Image Analyst
Image Analyst il 22 Ott 2013
Modificato: Image Analyst il 22 Ott 2013
Niamh, I knew this would happen. I think you accepted the wrong answer. Did you not notice that not all of the cycles were displayed in this answer? Only 3 instead of 97 like you'd expect to see in a one second time period. From what this Answer displays you'd think the frequency was 3 hertz, not 97 hertz! Did you run only this Answer and totally ignore mine? Did you not understand my explanation of aliasing and Nyquist frequency ? It would be wise of you to study up on that so you understand these very very important concepts.

Accedi per commentare.

Più risposte (1)

Image Analyst
Image Analyst il 22 Ott 2013
Modificato: Image Analyst il 22 Ott 2013
Hopefully this was just a question asked verbally in class and not an actual homework assignment. Wouldn't it just be something like this:
% Make 1000 samples in the range 0 to 1 second.
t = linspace(0, 1, 1000);
% Assign period and phase shift of 97 degrees.
period = 1/97; % 97 hertz = period of 1/97 of a second.
phaseShift = 97 * pi/180; % In radians.
y = sin(2*pi*t/period - phaseShift));
plot(t,y, 'bo-', 'LineWidth', 2);
grid on;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
Of course with 97 periods across the screen of width 1920 pixels, you won't see much - it will be all smashed together horizontally. You might want to plot fewer periods.

Categorie

Scopri di più su Get Started with MATLAB in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by