pgfplot domain with defined step size?

1,409

John's comment hints you may actually want the following

Also, a more simple way of doing, I think....

The output

enter image description here

The code

\documentclass[12pt,tikz,border=0pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
  \begin{tikzpicture}
    \begin{axis}
      [
        %axis lines = center,
        xmin=0,
        domain=0:.03,
        samples=201 % I find 101 is enough in print, 201 is better on screen
      ]
      \addplot [blue] {cos(deg(60*x))+cos(deg(60*5*x))+sin(deg(60*10*x))};
    \end{axis}
  \end{tikzpicture}
\end{document}

Orig

This seems to do the trick.

(I tried to remain faithful to the style of your setup)

The output

enter image description here

The code

\documentclass[12pt,tikz,border=0pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
  \def\Fs{8000}
  \pgfmathsetmacro{\dt}{1/\Fs}
  \def\StopTime{.03}
  \def\samplesAt{0,\dt,...,\StopTime-\dt}
    \begin{tikzpicture}
      \begin{axis}
        \addplot[samples at=\samplesAt] 
          {cos(2*3.14*60*x)+cos(2*3.14*60*5*x)+sin(2*3.14*60*10*x)};
      \end{axis}
    \end{tikzpicture}
\end{document}
Share:
1,409

Related videos on Youtube

dfh
Author by

dfh

Updated on August 01, 2022

Comments

  • dfh
    dfh over 1 year

    I am currently trying to display a sine wave, but can't seem to show it using the right steps size.

    Is it possible in pgfplots to determine the steps size of where the data points should be placed..

    So for instance if I have an audio signal which was sampled with 8000 hz.

    Each sample should be plotted given these specifications

       %% Time specifications:
       Fs = 8000;                   % samples per second
       dt = 1/Fs;                   % seconds per sample
       StopTime = 0.03;             % seconds
       t = (0:dt:StopTime-dt)';     % seconds
    

    But from what i can does pgfplot an option where i can a dt..

    My current plot...

    \begin{figure}
        \begin{tikzpicture}
            \begin{axis}
            \addplot[samples=240,domain=0:1] {
            cos(2*3.14*60*x)+cos(2*3.14*60*5*x)+sin(2*3.14*60*10*x)};
            \end{axis}
        \end{tikzpicture}
    \end{figure}
    

    question condensed: Is there someway I can incorporate the time specification specified above in the pgfplot.

    • marsupilam
      marsupilam about 6 years
      Take a look at the pgfplots keys samples=<num>, domain=<interval> and (maybe) samples at=<list>. Also this goes without saying, but it would be useful for potential answerers if you provided a working example (written in actual code).
    • Zarko
      Zarko about 6 years
      your "step size" is determined by number of samples ...
    • dfh
      dfh about 6 years
      ohh.. ahh cool :)
    • dfh
      dfh about 6 years
      Thanks for the reminder @JohnKormylo adding 180/pi seem to be the easiest solution..
    • John Kormylo
      John Kormylo about 6 years
      BTW, sin()_ and cos() use degrees, so multiplying by 2*3.14 is probably not a good idea. In addition, pi is a recognized constant, and every intermediate calculation will be repeated over and over.