Skip to content
Snippets Groups Projects
Commit 8dc722c6 authored by Lukas Müller's avatar Lukas Müller
Browse files

added use_line_collection to stem

parent 1caeedc8
Branches master
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# Übung 11 - Abtastung & Aliasing
In dieser Übung betrachten wir ein Signal $x(t)=\cos(2\pi\cdot f\cdot t)$ mit der Frequenz $f=10\,\text{Hz}$ , welches mit einer Frequenz $f_T$ abgetastet werden soll.
Um die bei diesem Unterfangen möglicherweise auftretenden Probleme zu visualisieren, setzen wir in diesem Notebook eine variierende Abtastfrequenz $f_T$ ein, die durch einen Slider variiert werden kann.
## Berechnung und Definition
%% Cell type:code id: tags:
``` python
from numpy import cos, pi, abs
import numpy
from scipy.fft import fft, fftshift
f = 10 # Hz
ft = 40 # Hz
duration = 1 # s
t = numpy.linspace(0, duration, 5000)
n = numpy.arange(0, duration*ft)
zeropadding = 2**10
Omega = numpy.linspace(-pi, pi, zeropadding)
def x_continuous(t, f):
return cos(2*pi*f*t)
def x_discrete(n, ft):
return cos(2*pi*f/ft*n)
```
%% Cell type:markdown id: tags:
## Visualisierung
%% Cell type:code id: tags:
``` python
from IPython.display import display
from ipywidgets import IntSlider, FloatSlider
from matplotlib import pyplot
%matplotlib notebook
ft_slider = IntSlider(min=1, max=f*5, layout={'width':'100%'}, value=2)
figure, (axis1, axis2) = pyplot.subplots(2, 1)
pyplot.tight_layout()
figure.set_figwidth(9.5)
figure.set_figheight(10)
x_result = x_continuous(t, f)
def slider_update_plot(changes):
ft = ft_slider.value # get the sampling frequency from the slider
n = numpy.arange(0, duration*ft)
dft = abs(fftshift(fft(x_discrete(n, ft), zeropadding)))/ft/duration
max_index = numpy.argmax(dft[0:int(zeropadding/2)])
omega_max = abs(Omega[max_index])
# visualize
axis1.cla()
axis1.set_xlim((0, 1))
axis1.plot(t, x_result)
axis1.plot(t, x_continuous(t, omega_max*ft/(2*pi)))
axis1.stem(n/ft, x_discrete(n, ft))
axis1.stem(n/ft, x_discrete(n, ft), use_line_collection=True)
axis1.set_xlabel('$t$ in s', fontsize=22)
axis1.set_yticks([-1, 0, 1])
axis2.cla()
axis2.set_xlim((-12, 12))
axis2.set_ylim((0, 1.1))
axis2.plot(Omega*ft/(2*pi), dft)
axis2.set_xticks(numpy.arange(-12, 13, 1))
axis2.set_ylabel('DFT{x[n]}', fontsize=22)
axis2.set_xlabel('$f$ in Hz', fontsize=22)
ft_slider.observe(slider_update_plot)
slider_update_plot(None)
display(ft_slider)
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment