Exploring digital signal processing using an interactive Jupyter notebook and smartphone accelerometer data
Full text
This is a self-archived version of an original article. This version may differ from the original in pagination and typographic details. Author(s): Title: Year: Version: Copyright: Rights: Rights url: Please cite the original version: CC BY-NC-ND 4.0 https://creativecommons.org/licenses/by-nc-nd/4.0/ Exploring digital signal processing using an interactive Jupyter notebook and smartphone accelerometer data © 2023 European Physical Society Accepted version (Final draft) Pirinen, Pekka; Klein, Pascal; Lahme, Simon Zacharias; Lehtinen, Antti; Rončević, Lucija; Susac, Ana Pirinen, P., Klein, P., Lahme, S. Z., Lehtinen, A., Rončević, L., & Susac, A. (2024). Exploring digital signal processing using an interactive Jupyter notebook and smartphone accelerometer data. European Journal of Physics, 45(1), Article 015802. https://doi.org/10.1088/1361-6404/ad0790 2024
European Journal of Physics ACCEPTED MANUSCRIPT Exploring digital signal processing using an interactive Jupyter notebook and smartphone accelerometer data To cite this article before publication: Pekka Pirinen et al 2023 Eur. J. Phys. in press https://doi.org/10.1088/1361-6404/ad0790 Manuscript version: Accepted Manuscript Accepted Manuscript is “the version of the article accepted for publication including all changes made as a result of the peer review process, and which may also include the addition to the article by IOP Publishing of a header, an article ID, a cover sheet and/or an ‘Accepted Manuscript’ watermark, but excluding any other editing, typesetting or other changes made by IOP Publishing and/or its licensors” This Accepted Manuscript is © 2023 European Physical Society. During the embargo period (the 12 month period from the publication of the Version of Record of this article), the Accepted Manuscript is fully protected by copyright and cannot be reused or reposted elsewhere. As the Version of Record of this article is going to be / has been published on a subscription basis, this Accepted Manuscript will be available for reuse under a CC BY-NC-ND 3.0 licence after the 12 month embargo period. After the embargo period, everyone is permitted to use copy and redistribute this article for non-commercial purposes only, provided that they adhere to all the terms of the licence https://creativecommons.org/licences/by-nc-nd/3.0 Although reasonable endeavours have been taken to obtain all necessary permissions from third parties to include their copyrighted content within this article, their full citation and copyright line may not be present in this Accepted Manuscript version. Before using any content from this article, please refer to the Version of Record on IOPscience once published for full citation and copyright details, as permissions may be required. All third party content is fully copyright protected, unless specifically stated otherwise in the figure caption in the Version of Record. View the article online for updates and enhancements. This content was downloaded from IP address 130.234.90.20 on 31/10/2023 at 09:50
Exploring digital signal processing using an interactive Jupyter notebook and smartphone accelerometer data P. Pirinen1, P. Klein2, S. Z. Lahme2, A. Lehtinen1,3, L. Ronˇcevi´c4, and A. Susac4 1Department of Physics, P.O. Box 35, 40014 University of Jyv¨askyl¨a, Finland 2Faculty of Physics, Physics Education Research, University of G¨ottingen, Friedrich-Hund-Platz 1, 37077, G¨ottingen, Germany 3Department of Teacher Education, P.O. Box 35, 40014 University of Jyv¨askyl¨a, Finland 4Department of Applied Physics, Faculty of Electrical Engineering and Computing, University of Zagreb, Unska 3, 10000, Zagreb, Croatia E-mail: [email protected] Abstract. Digital signal processing is a valuable practical skill for the contemporary physicist, yet in physics curricula, its central concepts are often introduced either in method courses in a highly abstract and mathematics-oriented manner or in lab work with little explicit attention. In this paper, we present an experimental task in which we focus on a practical implementation of the discrete Fourier transform (DFT) in an everyday context of vibration analysis using data collected by a smartphone accelerometer. Students are accompanied in the experiment by a Jupyter notebook companion, which serves as an interactive instruction sheet and a tool for data analysis. The task is suitable for beyond-first-year university physics students with some prior experience in uncertainty analysis, data representation, and data analysis. Based on our observations the experiment is very engaging. Students have consistently reported interest in the experiment and they have found it a good demonstration of the DFT method. 1. Introduction Digital signal processing is an important skill for physicists, with a multitude of uses in experimental physics (see, for example, Ref. [1] for signal processing in the famous LIGO-Virgo gravitational wave experiments) and industry (for example detecting faults in machinery via vibration analysis [2]). Some basic elements of digital signal processing are typically included implicitly in undergraduate laboratory courses, but learning objectives related to concepts and methods of signal processing are often hidden behind more explicit objectives related to reinforcing physics concepts. Page 1 of 14 AUTHOR SUBMITTED MANUSCRIPT - EJP-108123.R1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 Accepted Manuscript
2 We present an experimental task in which students analyze vibrations by performing a discrete Fourier transform (DFT) on data measured by a smartphone accelerometer. Students are accompanied in their investigation by a Jupyter notebook, which first introduces the concepts and methods relevant to the experiment and provides Python scripts for analyzing the collected data. Students ultimately measure the frequency components of an assumed periodic signal found at home that they want to investigate, either by picking one from our suggestions (for example the spin-dry rotation frequency of a washing machine or one’s own heart rate) or coming up with their own target for investigation. The task can be used to complement teaching the basics of digital signal processing in an everyday physics context to beyond-first-year university physics students, and it is perfectly suitable for a distance-learning setting although it can be conducted on campus as well. Physics lab tasks utilizing the DFT and its algorithm implementation fast Fourier transform (FFT) have existed for decades [3, 4]. Ref. [5] showcases several examples with electric circuits. Ref. [6] uses the FFT in a mechanics context to analyze the eigenfrequencies of coupled harmonic oscillators from force transducer data. In Ref. [7] a similar vibration analysis as in the present work was introduced utilizing computerassisted measurements and ready-made software for data analysis applied to studying the rotational frequency of a fan. Two experiments in optics and electromagnetism advocating the use of modern digital equipment such as iOLab units or microcontrollers were presented in Ref. [8]. In Ref. [9], LabVIEW was used to teach FFT, and in Ref. [10], electrical engineering students used Raspberry Pi microprocessor systems to collect digital data they analyzed afterward with FFT in Python, accompanied by an introduction to python and applying FFT during DSP. In Ref. [11], a Java-based interactive simulation tool is presented that electrical engineering students can use also in distance-learning settings to perform digital signal processing simulations, among them Fourier transforms. Smartphone data was used in conjunction with an FFT data analysis in an experiment on acoustic beat phenomena in Ref. [12]. The smartphone measurement app phyphox [13] contains a tool for continuously viewing the FFT of measured acceleration data [14], and the phyphox group has listed some short example experiments on their YouTube channel [15]. Jupyter notebooks are used in science education to, for example, provide interactive elements to a classroom [16], to teach basic subject-specific content while learning computational skills relevant to, e.g., data analysis or visualization [17,18], or to report a computational activity in a form of a computational essay [19]. The use of Jupyter notebooks in physics education is reviewed in Ref. [20] where research results supporting the use of interactive learning tools such as Jupyter notebooks are also presented. Thanks to smartphones, more and more people carry a basic kit for vibration analysis in their pockets everywhere they go. Smartphones are increasingly utilized in physics education, and in addition to being a widely accessible measuring device, they have pedagogical potential that can be utilized in combination with traditional Page 2 of 14AUTHOR SUBMITTED MANUSCRIPT - EJP-108123.R1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 Accepted Manuscript
3 physics experiments [21]. Using smartphones as experimental tools can increase interest in studying physics and curiosity in the experiment itself [22]. When on-campus experiments are not possible (for example, under pandemic conditions), experiments with mobile devices provide opportunities to collect first-hand data which has been shown to lead to higher students’ perceived learning success in comparison with using second-hand data [23]. However, another study on the secondary school level suggests that the origin of the data does not matter in achieving desired learning outcomes [24]. In the study of Ref. [25], the use of mobile devices for video motion analysis reduced extraneous cognitive load during experimentation. While the name of Fourier is usually tossed around in the context of analyzing sound frequencies, our choice to focus on vibration analysis via accelerometer data is based on making the measured quantities and measurement apparatus as simple as possible to not take attention away from the main learning objective: basic digital signal processing in data analysis. By this, we also promote the idea that there are lots of (everyday) applications for Fourier analysis beyond the conventional example of audio signals. To our knowledge, our experimental task is novel with the combination of smartphoneassisted data collection, analyzing frequencies from accelerometer data, and using a Jupyter notebook as a tool for data analysis. Students use Python code in the Jupyter notebook for data handling and performing the DFT, leaving them equipped with tools to perform a similar analysis on any data in other applications, which is often lacking in approaches where the DFT is computed in a pre-made tool and only the result is visualized. As part of this task, students practice planning an experiment within the relatively simple setup given in the task. Our experiment follows a skills-based approach to laboratory work, emphasizing the role of open inquiry and learning experimental skills such as experimental design and data analysis over cookbook-style closed instructions and reinforcing theory content. This approach has been shown to lead to beneficial outcomes both in learning critical thinking skills [26, 27] and in developing expert-like beliefs and attitudes about experimental physics [26–30]. This experimental task has been developed in the Erasmus+ project Developing Digital Physics Laboratory Work for Distance Learning (DigiPhysLab) [31]. The produced materials include task instructions for students, additional information and suggestions for instructors, and the accompanying Jupyter notebook. All materials can be accessed on our project website [32]. In Section 2 we superficially summarize the mathematical foundation for the DFT as given in the instructions of our experimental task. In Section 3 the Jupyter notebook companion is described. The experimental task and suggested possibilities for the investigation are presented along with two examples in Section 4, and observations from our implementations of this experiment are discussed in Section 5. Our experiences and findings are summarized in Section 6. Page 3 of 14 AUTHOR SUBMITTED MANUSCRIPT - EJP-108123.R1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 Accepted Manuscript
4 2. Discrete Fourier transform The experimental task does not focus on the mathematical subtleties of the DFT. Instead, we aim to give students an intuitive understanding of what the DFT does and to introduce how the method can be applied to a signal in practice, something that can often remain vague in more mathematically oriented approaches like mathematical method courses. For completeness, a brief overview of the relevant equations is given here in the same form as it appears in our instructions for the experimental task [32]. Let’s define a signal {xn}={x0, x1, . . . , xN−1}consisting of N data points (samples) taken at constant time intervals of Tsseconds each. The DFT of the signal is computed as Xk= N−1 X n=0 xne− i2π Nkn,(1) where each Xkis a complex number, and the set of {Xk}={X0, X1, . . . , XN−1} represents the signal in the frequency domain. For a continuous function, this would correspond to a transformation of a function of time to a function of frequency. The interpretation of the Fourier-transform coefficients Xkcan become clearer when we look at the inverse DFT: xn=1 N N−1 X k=0 Xkei2π Nkn,(2) where we can see that the original signal can be represented as a sum of complex sinusoid components, and Xkdescribes the amplitude and phase of each component. For students less experienced with complex exponential functions, one can use Euler’s formula to present Eqs. (1) and (2) in the form Xk= N−1 X n=0 xn·cos(2π Nkn)−i N−1 X n=0 xn·sin(2π Nkn),(3) xn=1 N N−1 X k=0 Xk·cos(2π Nkn) + i1 N N−1 X k=0 Xk·sin(2π Nkn).(4) By using the vector notation ck[n] = cos(2π Nkn) and sk[n] = sin(2π Nkn) we can write Xkand xnas dot products of vectors, highlighting the idea of the DFT comparing the N-point signal x to sinusoidal functions: Xk=x ·ck−ix ·sk,(5) xn=1 N X·cn+i1 N X·sn.(6) In this experimental task, we utilize the amplitude spectrum of the Fourier transform defined for a real-valued signal as Aj=2 N|Xj|, j = 0,1, . . . , N/2,(7) Page 4 of 14AUTHOR SUBMITTED MANUSCRIPT - EJP-108123.R1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 Accepted Manuscript
5 which tells us how strongly the frequency fjis present in the signal. Here {fj}= {f0, f1, . . . , fN/2}, where the frequencies are related to the sampling frequency fs= 1/Ts as fj=jfs N=j NTs ,(8) and the frequencies visible in the Fourier transform are limited by fmax =fs/2. This maximum visible frequency is referred to as the Nyquist frequency. Note that a realvalued signal of Nsamples in the time domain corresponds to N/2 + 1 physically meaningful frequencies in the frequency domain. Another worthwhile note is that the DFT views the signal in the time domain as periodic with a period of NTs, and the resulting DFT is also periodic in the frequency domain with a period of fs. 3. The interactive notebook companion To accompany the students on their journey to the basics of digital signal processing we developed a Jupyter notebook, which acts as a personal guide to the students throughout the experimental task. The notebook starts by introducing key concepts of a digital signal, such as sampling rate (sampling frequency) and signal length, via a simple sine wave example. A DFT is first applied to a simple sine function, then some random noise is added to simulate a more realistic signal, and finally, the student is asked to add a few different frequency components to the signal to see what the DFT can tell about a signal. An example snippet from the notebook is shown in Figure 1. By introducing new concepts, giving small exercises, and asking guiding questions, the notebook gently and gradually introduces the student to an intuitive understanding of what the DFT does and to the basics of Python programming as a tool for data analysis. In the Jupyter notebook, we also discuss aliasing, the phenomenon of a highfrequency component of a signal masquerading as a lower frequency in a sample taken with a sampling frequency too low to accurately capture the behavior of the signal. We demonstrate via an example that such a phenomenon can occur and students need to be wary of it while measuring and analyzing their data. Students can then choose to measure signals that are essentially upper-bound in frequency, but also measuring signals where aliasing becomes an issue can lead to fruitful discussions: see the example of smartphone vibrations in Section 4. Other aspects of digital signal processing, such as windowing or noise reduction, can be added depending on the scope of the course and context but they are not necessary for the investigations of this experimental task. Students learn about data handling when they produce a data file that can be read by the Python program for data analysis. Spreadsheets are used only for transporting the raw data from the smartphone to the computer and storing the results of computations, all analysis and plotting will be done using Python code in the notebook. Smartphone accelerometers used via a measuring app typically have a default sampling frequency of 100 – 400 Hz, which means that even in a short measurement there will be a lot of data points. Explicitly showing the data-handling steps in the Page 5 of 14 AUTHOR SUBMITTED MANUSCRIPT - EJP-108123.R1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 Accepted Manuscript
6 Figure 1: Snippet of the notebook companion to the experimental task. The placement of the figure output of the code was edited to make a more compact figure. Students are given small exercises in bold font, and the required edit to the code block is shown highlighted in pale blue. code of the Jupyter notebook promotes methods and practices to deal with datasets with thousands of data points instead of the dozen or so often taken in educational lab experiments as a minimum requirement for meaningful statistical analysis. Note that the number of data points to be analyzed via the FFT algorithm depends on the choice of the observation interval in the raw data from the accelerometer. Finally, the notebook provides instructions and hints for the experimental investigation of vibrations using the DFT method. Students are given the task of building their own code for data analysis by copying and modifying any parts of the Page 6 of 14AUTHOR SUBMITTED MANUSCRIPT - EJP-108123.R1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 Accepted Manuscript
7 Figure 2: Flowchart of the data handling in the experimental task. examples introduced in the notebook. The measurements and analysis involved in the experimental task are described in the next section via two example investigations. The workflow of data handling in the experimental task and the interplay between smartphone measurements and the Jupyter notebook is shown in Figure 2. The Jupyter notebook instruction also doubles as an example of a computational essay [19], which can be used as the format of a lab report for the students’ assessment. A computational essay is defined in Ref. [19] as ”a type of essay or report that explicitly incorporates live code to support its thesis, usually written in a notebook environment”. Therefore, a computational essay differs from a more traditional lab report in that it includes code and code output directly within the body of text, providing a natural way to describe and report a computational activity such as the data analysis of the experiment presented in this work. 4. The experimental task Equipped with the Jupyter notebook and necessary tools for data analysis, students are then free to tackle a problem of their choice. The task for the students is to determine the frequency or frequencies present in some periodic signal they can find at home or on campus by analyzing collected smartphone accelerometer data via a discrete Fourier transform. The design of the experiment and data collection are left open for the students to decide. Some possible contexts for investigation that we have offered, or our students have come up with, include the •vibrating alarm of a phone, •vibration of a computer (if it vibrates enough to be measured), •spin-dry rotation of a washing machine, •vibration inside a car due to the engine, Page 7 of 14 AUTHOR SUBMITTED MANUSCRIPT - EJP-108123.R1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 Accepted Manuscript
14 2021 Phys. Rev. Phys. Educ. Res. 17(1) 010117 URL https://link.aps.org/doi/10.1103/ PhysRevPhysEducRes.17.010117 [24] Priemer B, Pfeiler S and Ludwig T 2020 Phys. Rev. Phys. Educ. Res. 16(1) 013102 URL https://link.aps.org/doi/10.1103/PhysRevPhysEducRes.16.013102 [25] Becker S, Klein P, G¨oßling A and Kuhn J 2020 Learning and Instruction 69 101350 ISSN 09594752 URL https://linkinghub.elsevier.com/retrieve/pii/S0959475219305961 [26] Smith E M, Stein M M, Walsh C and Holmes N 2020 Physical Review X 10 011029 ISSN 2160-3308 URL https://link.aps.org/doi/10.1103/PhysRevX.10.011029 [27] Walsh C, Lewandowski H and Holmes N 2022 Physical Review Physics Education Research 18 010128 ISSN 2469-9896 URL https://link.aps.org/doi/10.1103/PhysRevPhysEducRes.18. 010128 [28] Wilcox B R and Lewandowski H J 2017 Phys. Rev. Phys. Educ. Res. 13(1) 010108 URL https://link.aps.org/doi/10.1103/PhysRevPhysEducRes.13.010108 [29] Kontro I, Heino O, Hendolin I and Galambosi S 2018 European Journal of Physics 39 025702 URL https://doi.org/10.1088/1361-6404/aa9364 [30] Henderson R, Funkhouser K and Caballero M D 2020 A longitudinal exploration of students’ beliefs about experimental physics 2019 Physics Education Research Conference Proceedings (Provo, UT: American Association of Physics Teachers) URL https://www.compadre.org/per/items/ detail.cfm?ID=15278 [31] Lahme S Z, Klein P, Lehtinen A, Pirinen P, Suˇsac A and Tomrlin B 2022 PhyDid B - Beitr¨age zur Fr¨uhjahrstagung - virtuell 2022 383–390 URL https://ojs.dpg-physik.de/index.php/ phydid-b/article/view/1250/1503 [32] The website of the DigiPhysLab project https://www.jyu.fi/digiphyslab accessed: 08.03.2023 [33] Kalender Z Y, Stump E, Hubenig K and Holmes N G 2021 Phys. Rev. Phys. Educ. Res. 17(2) 020128 URL https://link.aps.org/doi/10.1103/PhysRevPhysEducRes.17.020128 [34] Schmidt J A, Rosenberg J M and Beymer P N 2018 Journal of Research in Science Teaching 55 19–43 URL https://onlinelibrary.wiley.com/doi/abs/10.1002/tea.21409 [35] Lahme S Z, Klein P, Lehtinen A, Pirinen P, Ronˇcevi´c L and Suˇsac A 2023 Evaluating digital experimental tasks for physics laboratory courses (Preprint http://dx.doi.org/10.13140/ RG.2.2.26818.35526) Page 14 of 14AUTHOR SUBMITTED MANUSCRIPT - EJP-108123.R1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 Accepted Manuscript