0% found this document useful (0 votes)
7 views4 pages

MMF Altered

The document contains C++ code that implements a moving median filter. It generates random sample data, applies a median filter with a window size of 3, and prints the input and output. It includes headers for I/O, random number generation, algorithms, vectors, and concepts. A medianfilter function sorts and calculates the median of subsets of the data to perform the filtering.

Uploaded by

PSYCHO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

MMF Altered

The document contains C++ code that implements a moving median filter. It generates random sample data, applies a median filter with a window size of 3, and prints the input and output. It includes headers for I/O, random number generation, algorithms, vectors, and concepts. A medianfilter function sorts and calculates the median of subsets of the data to perform the filtering.

Uploaded by

PSYCHO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <iostream>

#include <random>
#include <algorithm>
#include <limits>
#include <iomanip>
#include <numbers>
#include <ranges>
#include <vector>
#include <concepts>
#include <type_traits>

template<typename T>
void pretty_print(T &iterator_able,std::string && st);

/*
Your code goes here
*/
std::vector <float> result;
template<typename t>
concept _isFloatingType = requires(t a)
{
requires std::is_floating_point<t>::value;
};

void medianfilter(double *arr, int N)


{
float temp;
float window[N];
for (int i = 0; i < 100 ;i++)
{
for (int j = 0; j < N; j++)
{
if ((i+N) > 100)
{
continue;
}
else
{
window[j] = arr[i + j];
}
}
for (int k=0;k<N;k++)
{
for (int l=k+1;l<N;l++)
{
if (window[k] > window[l])
{
temp=window[l];
window[l]=window[k];
window[k]=temp;
}
}
}
if ((i+N) <= 100)
{
if (N%2==0)
{
result.push_back((window[N/2]+window[(N/2)+1])/2);
}
else
{
result.push_back(window[N/2]);
}
}
}
}

int main()
{
std::random_device rd;
std::mt19937 twister(rd());
std::uniform_real_distribution<double> dis(-0.5,0.5);
std::array<double,100> samples;
for(auto& _a : samples)
_a = dis(twister);
pretty_print(samples,"Input Samples");
std::cout<<std::endl;
std::cout<<std::endl;

medianfilter(samples.data(),3);

pretty_print(result,"Moving Median Filter");


/*
Pretty print your vector into the function to see output
*/
}

template<typename T>
void pretty_print(T &iterator_able,std::string && st)
{
std::cout<<"-------"<<st<<"-------"<<std::endl;
int i =0;
for(auto & element:iterator_able)
{
if((i%5)==0)
std::cout<<std::endl;
if(element<0)
std::cout<<std::setprecision(2)<<element<<" ";
else
std::cout<<" "<<std::setprecision(2)<<element<<" ";
i++;
}
}

#include <iostream>
#include <random>
#include <algorithm>
#include <limits>
#include <iomanip>
#include <numbers>
#include <ranges>
#include <vector>
#include <concepts>
#include <type_traits>
template<typename T>
void pretty_print(T &iterator_able,std::string && st);

/*
Your code goes here
*/
std::vector <float> result;
template<typename t>
concept _isFloatingType = requires(t a)
{
requires std::is_floating_point<t>::value;
};

template<typename T,size_t size>


requires _isFloatingType<T>
void medianfilter(std::array<T,size> &arr, int N)
{
float temp;
float window[N];
for (int i = 0; i < 100 ;i++)
{
for (int j = 0; j < N; j++)
{
if ((i+N) > 100)
{
continue;
}
else
{
window[j] = arr[i + j];
}
}
for (int k=0;k<N;k++)
{
for (int l=k+1;l<N;l++)
{
if (window[k] > window[l])
{
temp=window[l];
window[l]=window[k];
window[k]=temp;
}
}
}
if ((i+N) <= 100)
{
if (N%2==0)
{
result.push_back((window[N/2]+window[(N/2)+1])/2);
}
else
{
result.push_back(window[N/2]);
}
}
}
}
int main()
{
std::random_device rd;
std::mt19937 twister(rd());
std::uniform_real_distribution<double> dis(-0.5,0.5);
std::array<double,100> samples;
for(auto& _a : samples)
_a = dis(twister);
pretty_print(samples,"Input Samples");
std::cout<<std::endl;
std::cout<<std::endl;

medianfilter(samples,3);

pretty_print(result,"Moving Median Filter");


/*
Pretty print your vector into the function to see output
*/
}

template<typename T>
void pretty_print(T &iterator_able,std::string && st)
{
std::cout<<"-------"<<st<<"-------"<<std::endl;
int i =0;
for(auto & element:iterator_able)
{
if((i%5)==0)
std::cout<<std::endl;
if(element<0)
std::cout<<std::setprecision(2)<<element<<" ";
else
std::cout<<" "<<std::setprecision(2)<<element<<" ";
i++;
}
}

You might also like