How do I properly plot data extracted from a scope as .csv file
<p>I just extracted a .csv file from a scope, which shows how a signal changes in the time duration of 6 seconds. Problem is that I can't come up with a proper way of plotting this signal, without things getting mushed together.</p>

<p>Mushed together like this:</p>

<p><a href="https://i.stack.imgur.com/XYYa9.png" rel="nofollow"><img src="https://i.stack.imgur.com/XYYa9.png" alt="Matplot screen shoot"></a></p>

<p>The .csv file is <a href="https://ufile.io/12a6" rel="nofollow">here</a>.</p>

<p>The signal goes through four stages, which I want to show with the plot, without it being to mushed together? How do I plot it?</p>

<p>More info about the signal:</p>

<p>The signal is a PWM signal that changes frequency. Either plotting the PWM signal vs. time, or the frequency of the PWM signal vs time, would be plot representation I would be /seeking for. </p>

<h2>Zoomable plot with Gnuplot</h2>

<p>Your problem is that you are plotting this with linear interpolation, with Gnuplot you plot digital data with the <code>with steps</code> style. If you use the <code>wxt</code> terminal (some other terminals also work) you get a zoomable plot, e.g.:</p>

<pre class="lang-gnuplot prettyprint-override"><code>set term wxt
set key above
plot 'foo.csv' with steps title columnhead
</code></pre>

<p>Results in:</p>

<p><img src="http://i.imgur.com/WqjEsi7.png" alt="Properly &quot;stepped&quot; pwm signal"></p>

<p>Or to plot a subsection of the data:</p>

<pre class="lang-gnuplot prettyprint-override"><code>set term wxt
set key above
set datafile separator comma
plot [1.7:1.8] [-.1:1.1] 'foo.csv' with steps title columnhead"
</code></pre>

<p>Output:</p>

<p><img src="http://i.imgur.com/nXBF0Lb.png" alt="Zoom in at 1.7 to 1.8"></p>

<h2>Determine the PWM frequency with awk</h2>

<p>As each line in your dataset represents a state switch, the switch frequency can be calculated by counting the switches and dividing by the difference in timestamps. This can be expressed in awk like this:</p>

<ol>
<li>Split data into <code>winsz</code> chunks</li>
<li>For each chunk output <code>winsz / delta_t</code></li>
</ol>

<p>Note I ignore the first two lines of the csv file.</p>

<pre><code>winsz=10

# Ignore heading and the first data point
tail -n+3 foo.csv | 

# Chunk data into winsz blocks
awk -F, 'NR % winsz == 0 { printf "\n" } 1' winsz=$winsz | 

# Output winsz
awk -F, 'NF &gt; 2 { print winsz / ($(NF-1) - $1)}' RS= winsz=$winsz &gt; foo-freq.txt
</code></pre>

<p>Here is a sample of <code>foo-freq.txt</code>:</p>

<pre><code>6.294413875000000 1237.47
6.303694208333334 1194.89
6.313335750000000 1150.17
6.323380625000000 1103.85
6.333885375000000 1055.28
6.344918833333334 1004.47
6.356571500000000 950.826
6.368958500000001 894.181
6.382239458333333 833.608
6.396642625000000 768.32
</code></pre>

<p>You can plot this with the following Gnuplot code:</p>

<pre class="lang-gnuplot prettyprint-override"><code>set xlabel 'Time (s)'
set ylabel 'Frequency (Hz)'
plot 'foo-freq.txt' with lines
</code></pre>

<p>Result:</p>

<p><img src="http://i.imgur.com/3kW1v3W.png" alt="PWM frequency plot"></p>

<h2>Determine the duty cycle with awk</h2>

<p>I know you did not ask for it, but here is how you can determine the duty cycle of the PWM with <code>awk</code>. You probably need to use GNU awk with multi-precision support, as you have 14 decimals in the sample timestamps.</p>

<p><em>duty-cycle.awk</em></p>

<pre class="lang-awk prettyprint-override"><code>NR == 1 { 
  start_time = time_stamp = $1
  next
} 

# Count the length of time the signal is 0 and 1 respectiviely
$2 == 0 { len0 += $1-time_stamp }
$2 == 1 { len1 += $1-time_stamp }

# Remember previous timestamp
{ time_stamp = $1 }

# How frequently to calculate and output the duty cycle
NR % winsz == 0 { 
  delta_t = time_stamp - start_time
  print len0 / delta_t, len1 / delta_t
  start_time = time_stamp
  len0 = len1 = 0
}
</code></pre>

<p>Run it like this on your data:</p>

<pre><code>tail -n+3 foo.csv | awk -M -F, -f duty-cycle.awk winsz=50 &gt; duty-cycle.txt
</code></pre>

<p>Result:</p>

<p><img src="http://i.imgur.com/GYyjHrp.png" alt="Plot of the calculated duty cycle"></p>

<h2>Datafile for future reference</h2>

<p>I have uploaded the datafile to a <a href="http://toggle.be/stackexchange/foo.csv" rel="nofollow">separate location</a> for future reference.</p>

python matlab csv plot gnuplot