You are on page 1of 11

ANALOG LOW PASS FILTER DESIGN BASED ON BUTTER

WORTH AND DIGITAL IIR BASED ON APPROXIMATION

MATLAB CODE:

% Sampling frequency (Hz)

Fs = 1000;

% Time duration of each cycle (seconds)

T = 1/50; % For a frequency of 50 Hz, T = 0.02 seconds

% Number of samples per cycle

N = T*Fs;

% Generate the input signal x(n) with 8 cycles of each frequency

frequencies = [50, 75, 100, 200, 300, 400, 500, 600];

x = [];

for freq = frequencies

% Generate one cycle of the sinusoidal signal

t = 0:1/Fs:T-1/Fs; cycle = sin(2*pi*freq*t);

% Add zeros between cycles

cycle_with_zeros = [cycle, zeros(1, N)];

% Concatenate cycles x = [x, repmat(cycle_with_zeros, 1, 8)];

end

% Digital filter parameters

b = [1, -1.3817, 1.5632, -0.8843, 0.4096, -0.1323, 0.0305, -0.0049, 0.0005];

a = [1, -0.7408, 0.9867, -0.5631, 0.2418, -0.0717, 0.0158, -0.0024, 0.0002];

% Apply the digital filter to the input signal y = filter(b, a, x);

% Butterworth filter parameters fp = 400;

% Passband frequency fs = 500;

% Stopband frequency Rp = 1;

% Passband ripple (dB) Rs = 40;

% Stopband attenuation (dB)

% Determine the filter order and cutoff frequency


[n, wn] = buttord(fp/(Fs/2), fs/(Fs/2), Rp, Rs, 's');

% Compute the filter coefficients

[b_butter, a_butter] = butter(n, wn, 's');

% Apply the Butterworth filter to the input signal

y_butter = filter(b_butter, a_butter, x);

% Plot the input and output signals for both filters

t = 0:1/Fs:length(x)/Fs-1/Fs; subplot(2,2,1);

plot(t, x);

xlabel('Time (seconds)');
ylabel('Amplitude');

title('Input Signal x(n)');

subplot(2,2,2);

plot(t, y);

xlabel('Time (seconds)');

ylabel('Amplitude');

title('Output Signal y(n) (digital filter)');

subplot(2,2,3); plot(t, x);

xlabel('Time (seconds)');

ylabel('Amplitude');

title('Input Signal x(n)');

subplot(2,2,4);

plot(t, y_butter);

xlabel('Time (seconds)');

ylabel('Amplitude');

title('Output Signal y(n) (Butterworth filter)');

OUTPUT :
Input Voltage Frequency (Hz) Output Voltage
9V 50 28V
9V 100 28.52V
9V 1.5k 28.42V
9V 3k 28.30
9V 6k 19.5
9V 10k 19.2
9V 100k 19.4

Code for Plot -

voltage = [28, 28.52, 28.42, 28.30, 19.5, 19.2, 19.4]; % voltage values

frequency = [50, 100, 1500, 3000, 6000, 10000, 100000]; % frequency values in Hz

% Convert voltage to gain in decibels (dB)


gain = 20*log10(voltage);

% Plot the graph

semilogx(frequency, gain);

xlabel('Frequency (Hz)');

ylabel('Gain (dB)');

title('Gain vs Frequency');

cutoff_frequency = 6000

hold on;

plot([cutoff_frequency, cutoff_frequency], [0, max(gain)], '--r');

text(cutoff_frequency*1.2, max(gain)*0.9, sprintf('Cutoff Frequency = %d Hz', cutoff_frequency), 'FontSize',

10, 'Color', 'r');

Plot -
STM CODE:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file stm32f4xx_it.c
* @brief Interrupt Service Routines.
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */

/* Includes */
#include "main.h"

#include "stm32f4xx_it.h"

/* Private includes */
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef */
/* USER CODE BEGIN TD */

/* USER CODE END TD */

/* Private define */
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro */
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables */
/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes */


/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code */


/* USER CODE BEGIN 0 */
extern ADC_HandleTypeDef hadc1;

extern DAC_HandleTypeDef hdac;

/* USER CODE END 0 */

/* External variables */
extern TIM_HandleTypeDef htim2;

/* USER CODE BEGIN EV */

/* USER CODE END EV */

/******************************************************************************/
/* Cortex-M4 Processor Interruption and Exception Handlers */
/******************************************************************************/
/**
* @brief This function handles Non maskable interrupt.
*/
void NMI_Handler(void)
{
/* USER CODE BEGIN NonMaskableInt_IRQn 0 */

/* USER CODE END NonMaskableInt_IRQn 0 */


/* USER CODE BEGIN NonMaskableInt_IRQn 1 */
while (1)

{
}
/* USER CODE END NonMaskableInt_IRQn 1 */
}

/**
* @brief This function handles Hard fault interrupt.
*/
void HardFault_Handler(void)
{
/* USER CODE BEGIN HardFault_IRQn 0 */
/* USER CODE END HardFault_IRQn 0 */
while (1)

{
/* USER CODE BEGIN W1_HardFault_IRQn 0 */
/* USER CODE END W1_HardFault_IRQn 0 */
}
}

/**
* @brief This function handles Memory management fault.
*/
void MemManage_Handler(void)
{
/* USER CODE BEGIN MemoryManagement_IRQn 0 */

/* USER CODE END MemoryManagement_IRQn 0 */


while (1)

{
/* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
/* USER CODE END W1_MemoryManagement_IRQn 0 */
}
}

/**
* @brief This function handles Pre-fetch fault, memory access fault.
*/
void BusFault_Handler(void)
{
/* USER CODE BEGIN BusFault_IRQn 0 */

/* USER CODE END BusFault_IRQn 0 */


while (1)

{
/* USER CODE BEGIN W1_BusFault_IRQn 0 */
/* USER CODE END W1_BusFault_IRQn 0 */
}
}

/**
* @brief This function handles Undefined instruction or illegal state.
*/
void UsageFault_Handler(void)
{
/* USER CODE BEGIN UsageFault_IRQn 0 */

/* USER CODE END UsageFault_IRQn 0 */


while (1)

{
/* USER CODE BEGIN W1_UsageFault_IRQn 0 */
/* USER CODE END W1_UsageFault_IRQn 0 */
}
}

/**
* @brief This function handles System service call via SWI instruction.
*/
void SVC_Handler(void)
{
/* USER CODE BEGIN SVCall_IRQn 0 */

/* USER CODE END SVCall_IRQn 0 */


/* USER CODE BEGIN SVCall_IRQn 1 */

/* USER CODE END SVCall_IRQn 1 */


}

/**
* @brief This function handles Debug monitor.
*/
void DebugMon_Handler(void)
{
/* USER CODE BEGIN DebugMonitor_IRQn 0 */

/* USER CODE END DebugMonitor_IRQn 0 */


/* USER CODE BEGIN DebugMonitor_IRQn 1 */

/* USER CODE END DebugMonitor_IRQn 1 */


}

/**
* @brief This function handles Pendable request for system service.
*/
void PendSV_Handler(void)
{
/* USER CODE BEGIN PendSV_IRQn 0 */

/* USER CODE END PendSV_IRQn 0 */


/* USER CODE BEGIN PendSV_IRQn 1 */

/* USER CODE END PendSV_IRQn 1 */


}

/**
* @brief This function handles System tick timer.
*/
void SysTick_Handler(void)
{
/* USER CODE BEGIN SysTick_IRQn 0 */

/* USER CODE END SysTick_IRQn 0


*/ HAL_IncTick();
/* USER CODE BEGIN SysTick_IRQn 1 */

/* USER CODE END SysTick_IRQn 1 */


}

/******************************************************************************/
/* STM32F4xx Peripheral Interrupt Handlers */
/* Add here the Interrupt Handlers for the used peripherals. */
/* For the available peripheral interrupt handler names, */
/* please refer to the startup file (startup_stm32f4xx.s). */
/******************************************************************************/

/**
* @brief This function handles TIM2 global interrupt.
* */
* void TIM2_IRQHandler(void)
* {
* /* USER CODE BEGIN TIM2_IRQn 0 */
* static float x_last[2], y_last[2];
* float x_current;
* float y_current;
* /* USER CODE END TIM2_IRQn 0 */
* HAL_TIM_IRQHandler(&htim2);
* /* USER CODE BEGIN TIM2_IRQn 1 */
* // HAL_GPIO_TogglePin(GPIOD,

GPIO_PIN_12);

HAL_GPIO_TogglePin(GPIOC,

GPIO_PIN_3);

* //gsw_toggle();
*
* unsigned short int x_current_adc = HAL_ADC_GetValue(&hadc1);
*
* // Current input digital value to analog
x_current = (float)
(x_current_adc*3.3/4095.0);
* y_current = 0.2928932*(x_current + 2 * x_last[1] +
x_last[0])- 0.1715729*y_last[0];
*
* // move last input sample (n-1) to make it (n-2) sample for
next ISR x_last[0] = x_last[1];
*
* // move current input sample to make it (n-1) sample for
next ISR x_last[1] = x_current;
*
* // Store the output calcu-
lated for y_last[0] =
y_last[1];
* y_last[1] = y_current;
*
* HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R,((y_current*4095)/
3.3));
*

* HAL_ADC_Start(&hadc1);
* /* USER CODE END TIM2_IRQn 1 */
* }
*

* /* USER CODE BEGIN 1 */


*
* /* USER CODE END 1 */
*
*
*
* INFERENCE / CONCLUSION:
* The amplitude versus frequency plot plays a crucial role in describing the
behavior of Infinite Impulse Response (IIR) filters. This plot displays the
amount of gain or attenuation of a filter at various frequencies, and it is used
to determine the appropriate filter coefficients to achieve the desired fre-
quency response. By analyzing this plot, one can gain insights into the fil-
ter's behavior, which is particularly useful in identifying any undesirable
characteristics that may need to be addressed. Therefore, the amplitude vs.
frequency plot is a powerful tool that enables engineers to optimize the de-
sign of IIR filters to meet specific performance criteria..

- Sarvesh sudhan p
- Pes1ug20ee056

You might also like