0% found this document useful (0 votes)
346 views5 pages

C Program Is A Low Pass Filter Design

This c program implements a 19-point finite impulse response (FIR) digital filter with a passband of 0.3-3.4 kHz and stopband of 800 Hz. It reads input data from a file, applies the FIR filtering, and writes the output to another file. The FIR filter coefficients are defined as a static float array. The main function opens the input and output files, initializes buffers to hold the input and output data, calls the filter function in a loop to process each frame of data, and closes the files.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
346 views5 pages

C Program Is A Low Pass Filter Design

This c program implements a 19-point finite impulse response (FIR) digital filter with a passband of 0.3-3.4 kHz and stopband of 800 Hz. It reads input data from a file, applies the FIR filtering, and writes the output to another file. The FIR filter coefficients are defined as a static float array. The main function opens the input and output files, initializes buffers to hold the input and output data, calls the filter function in a loop to process each frame of data, and closes the files.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

c program is a low pass filter for radio signal 0.3kHz-3.4kHz.

The stop
frequence of this filter is 800hz.This digtal filter chooses 19 points FIR
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
@end
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
@end
#import <Cocoa/Cocoa.h>
@interface ViewController : NSViewController
@end
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];

- (void)updateViewsWithFilteredAcceleration:(CMAcceleration)acceleration
static CGFloat x0 = 0;
static CGFloat y0 = 0;
const NSTimeInterval dt = (1.0 / 20);
const double RC = 0.3;
const double alpha = dt / (RC + dt);
CMAcceleration smoothed;
smoothed.x = (alpha * acceleration.x) + (1.0 - alpha) * x0;
smoothed.y = (alpha * acceleration.y) + (1.0 - alpha) * y0;
[self updateViewsWithAcceleration:smoothed];
x0 = smoothed.x;
y0 = smoothed.y;

}
}
- (void)setRepresentedObject:(id)representedObject {
[super setRepresentedObject:representedObject];

- (void)updateViewsWithAcceleration:(CMAcceleration)acceleration;
{
CGPoint center = self.view.center;
const CGFloat maxOffset = 200;
CGPoint frontCenter
acceleration.x),

= CGPointMake(center.x - (+1.0 * maxOffset *


center.y + (+1.0 * maxOffset *

acceleration.y));

CGPoint middleCenter = CGPointMake(center.x - (+0.2 * maxOffset *


acceleration.x),
center.y + (+0.2 * maxOffset *
acceleration.y));
CGPoint backCenter
acceleration.x),

= CGPointMake(center.x - (-1.0 * maxOffset *

acceleration.y));

center.y + (-1.0 * maxOffset *

self.frontView.center = frontCenter;
self.middleView.center = middleCenter;
self.backView.center = backCenter;
}
// Update the view, if already loaded.
}
@end

#include <stdio.h>
const int length = 180
void filter(int xin[ ],int xout[ ],int n,float h[ ]);
static float h[19]=
{0.01218354,-0.009012882,-0.02881839,-0.04743239,-0.04584568,
-0.008692503,0.06446265,0.1544655,0.2289794,0.257883,
0.2289794,0.1544655,0.06446265,-0.008692503,-0.04584568,
-0.04743239,-0.02881839,-0.009012882,0.01218354};
static int x1[length+20];
void
{

filter(int xin[ ],int xout[ ],int n,float h[ ])

int i,j;
float sum;
for(i=0;i<length;i++) x1[n+i-1]=xin[i];
for (i=0;i<length;i++)
{
sum=0.0;
for(j=0;j<n;j++) sum+=h[j]*x1[i-j+n-1];
xout[i]=(int)sum;
}
for(i=0;i<(n-1);i++) x1[n-i-2]=xin[length-1-i];
}
void
{

main( )
FILE
*fp1,*fp2;
int
frame,indata[length],outdata[length];
fp1=fopen(insp.dat,"rb");

fp2=fopen(outsp.dat,"wb");
frame=0;
while(feof(fp1)==0)
{
frame++;
printf("frame=%d\n",frame);
for(i=0;i<length;i++) indata[i]=getw(fp1);
filter(indata,outdata,19,h);
for(i=0;i<length;i++)
fcloseall( );
return(0);
}

- (float) getVolume
{
float
b_vol;
OSStatus
err;
MediaDeviceID
device;
UInt32
size;
UInt32
channels[5];
float
volume[2];
// get device
size = sizeof device;

putw(outdata[i],fp2);

err = MediaObjectGetPropertyData(kMediaHardwarePropertyDefaultOutputDevice,
&device, <#UInt32 inQualifierDataSize#>, <#const void *inQualifierData#>,
&size, <#void *outData#>);
if(err!=noErr)
{
NSLog(@"Media-volume error get device");
return 0.0;
}
// try set master volume (channel 0)
size = sizeof b_vol;
err = MediaDeviceGetProperty(device, 0, 0, kMediaDevicePropertyVolumeScalar,
&size, &b_vol);
//kMediaDevicePropertyVolumeScalarToDecibels
if(noErr==err) return b_vol;
// otherwise, try seperate channels
// get channel numbers 1-5
size = sizeof(channels);
err = MediaDeviceGetProperty(device, 0,
0,kMediaDevicePropertyPreferredChannelsForStereo, &size,&channels);
if(err!=noErr) NSLog(@"error getting channel-numbers");
size = sizeof(float);
err = MediaDeviceGetProperty(device, channels[0], 0,
kMediaDevicePropertyVolumeScalar, &size, &volume[0]);
if(noErr!=err) NSLog(@"error getting volume of channel %d",channels[0]);
err = MediaDeviceGetProperty(device, channels[1], 0,
kMediaDevicePropertyVolumeScalar, &size, &volume[1]);
if(noErr!=err) NSLog(@"error getting volume of channel %d",channels[1]);
b_vol = (volume[0]+volume[1])/2.00;
return
}

b_vol;

- (void)setVolume:(float)involume {
OSStatus
err;
MediaDeviceID
device;
UInt32
size;
Boolean
canset
= false;
UInt32
channels[2];
//float
volume[2];
// get default device
size = sizeof device;
printf("setVolume:: value of the volume set =%lf", involume);
err = MediaHardwareGetProperty(kMediaHardwarePropertyDefaultOutputDevice,
&size, &device);
if(err!=noErr) {
NSLog(@"Media-volume error get device");
return;
}
// try set master-channel (0) volume
size = sizeof canset;

err = MediaDeviceGetPropertyInfo(device, 0, false,


kMediaDevicePropertyVolumeScalar, &size, &canset);
if(err==noErr && canset==true) {
size = sizeof involume;
err = MediaDeviceSetProperty(device, NULL, 0, false,
kMediaDevicePropertyVolumeScalar, size, &involume);
return;
}
// else, try seperate channels
// get channels
size = sizeof(channels);
err = MediaDeviceGetProperty(device, 0, false,
kMediaDevicePropertyPreferredChannelsForStereo, &size,&channels);
if(err!=noErr) {
NSLog(@"error getting channel-numbers");
return;
}
// set
size = sizeof(float);
err = MediaDeviceSetProperty(device, 0, channels[0], false,
kMediaDevicePropertyVolumeScalar, size, &involume);
if(noErr!=err) NSLog(@"error setting volume of channel %d",channels[0]);
err = MediaDeviceSetProperty(device, 0, channels[1], false,
kMediaDevicePropertyVolumeScalar, size, &involume);
if(noErr!=err) NSLog(@"error setting volume of channel %d",channels[1]);
}

You might also like