1. For FIR filter having the specified impulse response h[n] determine the specified output y[n] for

a given input sequence x[n]. Show your work.

h[n] = {-1, 2, -1}
x[n] = {2, 6, 7, 3, 4, 1}

To find the output sequence y[n] for the given input sequence x[n] and specified impulse response h[n], we can use the convolution sum formula.

The convolution sum formula is given by:
y[n] = Σ h[k]*x[n-k]

where,
h[n] = {-1, 2, -1} is the impulse response
x[n] = {2, 6, 7, 3, 4, 1} is the input sequence

Now, we will calculate y[n] step by step:

For n = 0,
y[0] = h[0]*x[0] = -1*2 = -2

For n = 1,
y[1] = h[0]*x[1] + h[1]*x[0] = -1*6 + 2*2 = -6 + 4 = -2

For n = 2,
y[2] = h[0]*x[2] + h[1]*x[1] + h[2]*x[0] = -1*7 + 2*6 - 1*2 = -7 + 12 - 2 = 3

For n = 3,
y[3] = h[0]*x[3] + h[1]*x[2] + h[2]*x[1] = -1*3 + 2*7 - 1*6 = -3 + 14 - 6 = 5

For n = 4,
y[4] = h[0]*x[4] + h[1]*x[3] + h[2]*x[2] = -1*4 + 2*3 - 1*7 = -4 + 6 - 7 = -5

For n = 5,
y[5] = h[1]*x[4] + h[2]*x[3] = 2*4 - 1*3 = 8 - 3 = 5

Therefore, the output sequence y[n] = {-2, -2, 3, 5, -5, 5}

2. Configure the ADC in 12-bit right-aligned, single-channel, continuous conversion mode. Enable

channel 2 of the microcontroller DAC. Configure timer TIM7 to generate interrupts with the
specified sampling rate fs. In TIM7 period-elapsed ISR Implement FIR filter with the specified
coefficients (get the ADC conversion result, calculate convolution sum to determine your filter
output, and update DAC data register with the result).
Use 12-bit right-aligned mode for DAC output.
Set the function generator to generate 0-3V peak-to-peak (positively shifted 1.5V DC) sinusoidal
signal with the frequency of 100 Hz.
Before connecting a function generator to your development kit, connect the output of the
function generator directly to the oscilloscope and verify that your signal is 0-3V peak-to-peak,
positively shifted (1.5V DC) sine wave with the specified frequency.
Connect the output of the function generator to the ADC input via 1k resistor. Run the program on STM32 CUBE IDE and observe the DAC output with an oscilloscope.

fs = 22050 Hz
Coefficients: -0.000687, -0.003032, -0.005950, -0.009069, -0.011849, -0.013635, -0.013726, -0.011477, -0.006394, 0.001766, 0.012928, 0.026666, 0.042211, 0.058509, 0.074320, 0.088344, 0.099373, 0.106420, 0.108844, 0.106420, 0.099373, 0.088344, 0.074320, 0.058509, 0.042211, 0.026666, 0.012928, 0.001766, -0.006394, -0.011477, -0.013726, -0.013635, -0.011849, -0.009069, -0.005950, -0.003032, -0.000687

To implement the FIR filter with the specified coefficients using the STM32 microcontroller, you will need to follow these steps:

1. Configure the ADC in 12-bit right-aligned, single-channel, continuous conversion mode:
```c
// Configure ADC
HAL_ADC_Init(&hadc1);
ADC_ChannelConfTypeDef sConfig = {0};
sConfig.Channel = ADC_CHANNEL_2; // Channel 2 of the ADC
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
HAL_ADC_Start(&hadc1);
```

2. Configure timer TIM7 to generate interrupts with the specified sampling rate fs:
```c
// Configure TIM7
HAL_TIM_Base_Start_IT(&htim7);
__HAL_TIM_SET_AUTORELOAD(&htim7, 1000); // Set the period for fs=22050Hz
```

3. Implement FIR filter in TIM7 period-elapsed ISR:
```c
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim->Instance == TIM7)
{
// Get ADC conversion result
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
uint16_t adc_value = HAL_ADC_GetValue(&hadc1);

// Calculate convolution sum for FIR filter
float result = 0.0;
for (int i = 0; i < FILTER_LENGTH; i++)
{
result += FIR_coefficients[i] * buffer[(buffer_index + i) % BUFFER_SIZE];
}

// Update DAC data register with the result
HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, (uint32_t)result);

// Update buffer with ADC value
buffer[buffer_index] = adc_value;
buffer_index = (buffer_index + 1) % BUFFER_SIZE;
}
}
```

4. Set the FIR coefficients in your code:
```c
#define FILTER_LENGTH 36
float FIR_coefficients[FILTER_LENGTH] = {-0.000687, -0.003032, -0.005950, -0.009069, -0.011849, -0.013635, -0.013726, -0.011477, -0.006394, 0.001766, 0.012928, 0.026666, 0.042211, 0.058509, 0.074320, 0.088344, 0.099373, 0.106420, 0.108844, 0.106420,
0.099373, 0.088344, 0.074320, 0.058509, 0.042211, 0.026666, 0.012928, 0.001766, -0.006394, -0.011477, -0.013726, -0.013635, -0.011849, -0.009069, -0.005950, -0.003032, -0.000687};
```

5. Connect the output of the function generator into the ADC input through a 1k resistor and run the program on STM32 CUBE IDE. Monitor the DAC output with an oscilloscope.

Make sure to adjust the buffer size and sampling frequency to suit your specific requirements. Additionally, you may need to configure the DAC and the ADC according to your microcontroller's specifications.

3. Increase the input frequency with 100 Hz increment up to 3 kHz and determine the frequencies

corresponding to –3 dB and –6 dB attenuation.

To determine the frequencies corresponding to -3 dB and -6 dB attenuation as the input frequency is increased with a 100 Hz increment up to 3 kHz, we need to analyze the frequency response of the FIR filter.

1. Calculate the filter response:
You can calculate the frequency response of the FIR filter using the provided coefficients. You can use tools like MATLAB or Python to perform the frequency response analysis. Calculate the magnitude response of the filter for varying input frequencies ranging from 100 Hz to 3000 Hz in steps of 100 Hz.

2. Determine the frequencies corresponding to -3 dB and -6 dB attenuation:
-3 dB attenuation corresponds to a magnitude decrease of 0.707, and -6 dB attenuation corresponds to a magnitude decrease of 0.5. From the magnitude response obtained in step 1, identify the frequencies at which the magnitude drops to approximately 0.707 for -3 dB attenuation and 0.5 for -6 dB attenuation. These frequencies will correspond to the -3 dB and -6 dB cutoff frequencies of the FIR filter.

3. Interpolation:
Since you're increasing the input frequency in steps of 100 Hz, you may need to perform interpolation to determine the exact frequency values at which the magnitude reaches -3 dB and -6 dB attenuation.

By following these steps, you can determine the frequencies corresponding to -3 dB and -6 dB attenuation as the input frequency is increased with a 100 Hz increment up to 3 kHz.

4. Configure pin PG2 as output. In TIM7 ISR set PG2 pin before starting the computation of the filter

output and reset it in the end of the timer ISR. Observe the state of pin PG2 with oscilloscope to
determine computation time T1.

To configure pin PG2 as an output and observe the computation time T1 using an oscilloscope in the TIM7 ISR, you can follow these steps:

1. Configure pin PG2 as output:
```c
// Configure pin PG2 as output
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
```

2. Modify the TIM7 period-elapsed ISR to set and reset pin PG2 before and after the FIR filter computation:
```c
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim->Instance == TIM7)
{
// Set pin PG2 high before starting computation
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_2, GPIO_PIN_SET);

// Start computation of FIR filter output
// Calculate convolution sum for FIR filter
float result = 0.0;
for (int i = 0; i < FILTER_LENGTH; i++)
{
result += FIR_coefficients[i] * buffer[(buffer_index + i) % BUFFER_SIZE];
}

// Update DAC data register with the result
HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, (uint32_t)result);

// Update buffer with ADC value
buffer[buffer_index] = adc_value;
buffer_index = (buffer_index + 1) % BUFFER_SIZE;

// Reset pin PG2 at the end of the computation
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_2, GPIO_PIN_RESET);
}
}
```

3. Measure the computation time T1 using an oscilloscope:
Connect the oscilloscope probe to pin PG2 and trigger it to capture the rising edge when the pin is set high at the start of the FIR filter computation. Measure the time difference between the rising edge and the falling edge when the pin is reset low at the end of the computation. This time difference will be the computation time T1.

By setting and resetting pin PG2 within the TIM7 ISR, you can observe the computation time T1 using an oscilloscope. Remember to consider any system or peripheral delays that may affect the measurement.

5. Show your settings and calculations of timer TIM7 prescaler and auto-reload registers that you

used in question 2 to implement required sampling rate fs.

Prescaler = 79

To calculate the settings for the TIM7 prescaler and auto-reload register to achieve the required sampling rate of fs = 22050 Hz, with a specified system clock frequency, you can use the following formula:

TIM7 Frequency (Hz) = TIM7 Clock / ((prescaler + 1) * (auto-reload + 1))

Given:
- Required sampling rate fs = 22050 Hz
- System clock frequency = 72 MHz
- Prescaler = 79

First, calculate the desired TIM7 frequency based on the sampling rate:
22050 Hz = 72,000,000 Hz / ((79 + 1) * (auto-reload + 1))

Rearrange the formula to solve for the auto-reload register value:

auto-reload = (72,000,000 Hz / (22050 Hz * 80)) - 1
auto-reload = 29

Therefore, with a prescaler value of 79 and an auto-reload value of 29, you would achieve the required sampling rate of 22050 Hz for TIM7.

You can configure TIM7 with these settings in your STM32 CUBE IDE project to ensure that the sampling rate is correctly set for the FIR filter implementation as described in question 2.