\documentclass[12pt,letterpaper]{article}

% ---- Standard packages only ----
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{amsmath,amssymb,amsthm}
\usepackage{geometry}
\usepackage{graphicx}
\usepackage{fancyhdr}
\usepackage{enumitem}
\setlist[itemize]{itemsep=1pt,topsep=2pt,parsep=1pt,partopsep=0pt}
\usepackage{xcolor}
\usepackage{listings}
\usepackage[colorlinks=true,linkcolor=blue,citecolor=blue,urlcolor=blue]{hyperref}

% ---- Page layout ----
\geometry{margin=2.5cm,headheight=14pt,headsep=8pt}
\setlength{\parindent}{0pt}
\setlength{\parskip}{3pt plus 1pt}

% ---- Header / footer ----
\pagestyle{fancy}
\fancyhf{}
\rhead{\small\coursecode\ --- \studentname}
\lhead{\small Homework \hwnumber}
\rfoot{Page \thepage}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}

% ---- Metadata (edit these) ----
\newcommand{\coursecode}{Phys 256}
\newcommand{\hwnumber}{7}
\newcommand{\studentname}{Student Name}

% ---- Python listings style ----
\definecolor{codebg}{rgb}{0.97,0.97,0.95}
\definecolor{codekey}{rgb}{0.20,0.40,0.70}
\definecolor{codecom}{rgb}{0.40,0.40,0.40}
\definecolor{codestr}{rgb}{0.60,0.10,0.10}

\lstdefinestyle{pythonstyle}{
  language=Python,
  backgroundcolor=\color{codebg},
  basicstyle=\ttfamily\small,
  keywordstyle=\color{codekey}\bfseries,
  commentstyle=\color{codecom}\itshape,
  stringstyle=\color{codestr},
  showstringspaces=false,
  breaklines=true,
  frame=single,
  rulecolor=\color{codecom},
  numbers=left,
  numberstyle=\tiny\color{codecom},
  stepnumber=1,
  numbersep=8pt,
  tabsize=4,
  captionpos=b
}
\lstset{style=pythonstyle}

% ---- \problem and \solution commands ----
\newcounter{problem}
\makeatletter
\newcommand{\problem}[1]{%
  \refstepcounter{problem}%
  \vspace{6pt}\noindent{\fontsize{14pt}{16pt}\bfseries Problem \theproblem: #1}%
  \par\nobreak\vspace{3pt}}
\newcommand{\solution}{%
  \vspace{-2pt}%
  {\fontsize{14pt}{16pt}\bfseries\noindent Solution\par\nobreak\vspace{2pt}}}
\makeatother

% ---- Custom compact title (redefines \maketitle) ----
\makeatletter
\renewcommand{\maketitle}{%
  \thispagestyle{empty}%
  \noindent{\Large\bfseries \coursecode\ --- Homework \hwnumber}%
  \par\vspace{2pt}%
  \noindent{\studentname}%
  \par\vspace{8pt}%
  \hrule\vspace{10pt}}
\makeatother

\begin{document}
\maketitle
%---------------------------------------------------------------
\section*{Instructions}
\begin{itemize}
  \item You may use AI assistants to help write, test, and debug code, but you must understand your implementation and use your own scientific judgment to assess its validity. \textbf{Do not use AI to write the report.}
  \item Submit a PDF report containing in-line code snippets, plots, numerical results, explanations, and descriptions of relevant test cases. Submit your Python code separately.
  \item In your report, we are looking for you to \textbf{exercise and demonstrate your independent scientific judgment.} Thoughtful reports with careful, critical discussion of the results will receive a higher grade than those that simply run the AI-generated code and paste in the results.
  \item Submit each coded problem in a separate file named \lstinline{p1.py}, \lstinline{p2.py}, etc. You may import functions from previous problems. Include all necessary files in one ZIP archive.
  \item Use NumPy, SciPy, and Matplotlib where appropriate. Fix random seeds or specify them so that stochastic results can be reproduced. Label all plot axes.
\end{itemize}

\vspace{0.25in}

\problem{Problem 1 (3 points)}
%---------------------------------------------------------------
Have a look at the particular realization of the  $N$ point forward DFT
with the omitted  normalization coefficient:
\begin{eqnarray*}
	C_n=\sum_{k=0}^{N-1} y_k \exp( -i 2 \pi  k n /N)
\end{eqnarray*}

Analytically prove that the forward discrete Fourier transform is
periodic, i.e.,
$c_{n+N}=c_{n}$.
Note: recall that $\exp(\pm i 2 \pi)=1$.

Does this also prove  that $c_{-n} = c_{N-n}$? Prove your point.

\problem{Problem 2 (2 points)}
\label{p:fft_real}
%---------------------------------------------------------------
Use proof for the previous problem relationships and show that
the following relationship holds
for any sample set $\{y_0, y_1, \cdots y_{N-1}\}$ which has only real values (i.e., no complex part)
\begin{eqnarray*}
	c_n = c_{N-n}^*
\end{eqnarray*}
Where $^*$ depicts the complex conjugation.

\problem{Problem 3 (10 points)}
%---------------------------------------------------------------
Load the data from the file \lstinline{'hw_data_for_filter.csv'} provided at the class web page. 
It contains a table with $y$ vs
$t$ data points (the first column holds the time, the second holds $y$). These data points are
taken with the same sampling rate.

\begin{enumerate}
\item {(1 points)}
What is the sampling rate?

\item {(1 points)}
What is the lowest frequency (in Hz) in this data set which we
can scientifically discuss?

\item {(2 points)}
What is the largest frequency (in Hz) in this data set which we
can scientifically discuss?

\item {(2 points)}
Make a function which convert index $n$ to its corresponding frequency in Hz. (Do not use
library implementation from the \lstinline{fft} package). Recall that it
should have positive and negative frequencies. Save you function to file
\lstinline{'p3.py'} and submit to Gradescope.

\lstinputlisting[caption={\lstinline{p3.py} template for problem 3}, label={lst:p3-solution-code}]{hw_code_template//p3.py}

\item {(2 points)}
Calculate forward DFT of the data (use python \lstinline{np.fft.fft} package)
and plot the resulting {\bf amplitude} spectrum vs {\bf frequency} of the
spectral component in~{Hz}. Since it is the complex spectrum, you should
have positive and negative frequencies for your plot.


\item {(2 point)}
Based on calculated spectrum. Does the relationship from the problem~\ref{p:fft_real} hold true?

\end{enumerate}

Provide relevant listings and plots with your report.

\problem{(5 points)}
%---------------------------------------------------------------
Study the spectrum from the above data file \lstinline{'hw_data_for_filter.csv'}.

\begin{enumerate}
\item {(1 points)}
Name two  positive frequency components of the spectrum (measured in Hz not rad${}^{-1}$) which has the largest amplitudes.

\item {(1 points)}
Compare the amplitude of the largest component of the spectrum to the
apparent size of the signal in the time domain. Does it make sense, how
would you scale it based on number of samples so it is more reasonable?


\item {(1 points)}
Consider everything else but above 2 components of the DFT as noise.
Construct a low-pass filter  which will pass these two components. Plot the
filter frequency representation (positive and negative frequency). Explain your
choice of the filter and its parameters.

\item {(2 points)}
Apply the filter to the data Fourier representation and calculate the inverse
DFT. Plot the resulting filtered data representation and  raw data points
in the same plot. Does your filter completely get rid of noise? If not why is it so?
\end{enumerate}
Provide relevant listings and plots with your report.


\end{document}
