Skip to content

Commit

Permalink
Fixed up histogram to use magnitude, added magnitude and phase polar …
Browse files Browse the repository at this point in the history
…components
  • Loading branch information
Brian Tabone committed Jul 6, 2018
1 parent 9016f6b commit 96bc834
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 24 deletions.
33 changes: 27 additions & 6 deletions SampleGenerator/Sample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* */
using SignalGenerator;
using SignalProcessor;
using SignalProcessor.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -35,20 +36,40 @@ public Sample(int sampleRate, double seconds, double hertz, ISignalGenerator sig
_seconds = seconds;
_hertz = hertz;

int sampleCount = (int)Math.Ceiling(_sampleRate * _seconds);
int cycleCount = (int)Math.Ceiling(_seconds * _hertz);


if (sigGen != null)
{
_samples = sigGen.GetSignal(sampleCount, cycleCount);
_samples = sigGen.GetSignal(SampleCount, CycleCount);
}
else
{
_samples = new List<double>(cycleCount); // Create empty sample list
_samples = new List<double>(CycleCount); // Create empty sample list
}
_sampleIDX = 0;
}

public int SampleRate
{
get
{
return _sampleRate;
}
}
public int SampleCount
{
get
{
return (int)Math.Ceiling(_sampleRate * _seconds);
}
}

public int CycleCount
{
get
{
return (int)Math.Ceiling(_seconds * _hertz);
}
}
public FrequencyDomain GetFrequencyDomainForSlice
{
get
Expand All @@ -68,7 +89,7 @@ public List<Double> GetSignalSlice
// 64 channel DFT done on 50 samples + 14 zeros padded right
public List<double> Get50Padded64ChannelSamples()
{
int numSamplesToGet = 32;
int numSamplesToGet = 64;
int curIDX = _sampleIDX;
if (curIDX + numSamplesToGet >= _samples.Count)
{
Expand Down
17 changes: 17 additions & 0 deletions SignalProcessor/DFT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ public double ScaledImaginaryComponent(int K)

return (ImaginaryComponent[K] * ScalingFactor[K]);
}


// See page 162, Equation 8-6 and 8-7 in "The Scientist and Engineer's Guide to Digital Signal Processing
#region polarcoords
public double Magnitude(int k)
{
return Math.Sqrt(Math.Pow(ScaledRealComponent(k), 2) + Math.Pow(ScaledImaginaryComponent(k), 2));
}

public double Phase(int k)
{
if (ScaledRealComponent(k) == 0)
return Math.PI / 2.0;

return Math.Atan(ScaledImaginaryComponent(k) / ScaledRealComponent(k));
}
#endregion
}

public class DFT
Expand Down
4 changes: 4 additions & 0 deletions Signals And Transforms/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
<ComboBoxItem Name="HighPass">High</ComboBoxItem>
<ComboBoxItem Name="None">None</ComboBoxItem>
</ComboBox>

<Label Margin="10,0,0,0" Content="Clip Percent:" Foreground="Black" FontWeight="Bold" VerticalAlignment="Center"/>
<TextBox HorizontalAlignment="Left" Height="20" Width="40" Margin="5,0,0,0" KeyUp="TextBox_KeyEnterUpdate" Text="{Binding FilterClipPercentage}" VerticalAlignment="Center"/>

</StackPanel>
<views:HistogramView VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Row="1"/>
</Grid>
Expand Down
10 changes: 5 additions & 5 deletions Signals And Transforms/Models/SampleData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,26 @@ public static Sample SignalSample

Sample sinusoidSamp = new Sample(16000, 1, 500, sinusoid);
Sample sinusoidSamp2 = new Sample(16000, 1, 7000, sinusoid);
Sample whiteNoise = new Sample(16000, 1, 2000, random);
Sample whiteNoise = new Sample(16000, 1, 1000, random);
Sample squareWave = new Sample(8000, 1, 400, square);
_signalSample = sinusoidSamp.SumWithSample(sinusoidSamp2);
_signalSample = sinusoidSamp.SumWithSample(whiteNoise);
}

_signalSample.GetNextSamplesForTimeSlice(20); // Prime the pump
_signalSample.Get50Padded64ChannelSamples(); // Prime the pump
return _signalSample;
}
}
}

public static void SetFilter(PASSTYPE _filterType)
public static void SetFilter(PASSTYPE _filterType, double clipPercent)
{
if (_filterType == PASSTYPE.NONE)
{
_filter = null;
}
else
{
_filter = new SimplePassFilter(0.15, _filterType);
_filter = new SimplePassFilter(clipPercent, _filterType);
}
}

Expand Down
44 changes: 34 additions & 10 deletions Signals And Transforms/View Models/GraphViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class GraphViewModel : INotifyPropertyChanged
int _cycleCount;
private string _filterType;
int _framesPerSecond = 0;

private int _filterClipPercentage;

public event PropertyChangedEventHandler PropertyChanged;

Expand All @@ -38,6 +38,9 @@ public GraphViewModel()
_signalScale = 1.0;
_filterType = "None";
_dispatchTimer = new DispatcherTimer();
_signal = new List<double>(1000);
_synthesis = new List<double>(1000);

FramesPerSecond = 10;
GetNewModel();

Expand Down Expand Up @@ -101,25 +104,29 @@ private void GetNewModel()
switch (_filterType)
{
case "Low":
SampleData.SetFilter(PASSTYPE.LOW);
SampleData.SetFilter(PASSTYPE.LOW, _filterClipPercentage/100.0);
break;
case "High":
SampleData.SetFilter(PASSTYPE.HIGH);
SampleData.SetFilter(PASSTYPE.HIGH, _filterClipPercentage/100.0);
break;
default:
SampleData.SetFilter(PASSTYPE.NONE);
SampleData.SetFilter(PASSTYPE.NONE, _filterClipPercentage/100.0);
break;
}

_signal = SampleData.Get50Padded64ChannelSamples();
_frequencyDomain = SampleData.FreqDomain;

_synthesis = DFT.Synthesize(_frequencyDomain);
_synthesis.Clear();
_signal.Clear();
while (_signal.Count < 1000)
{
_signal.AddRange(SampleData.Get50Padded64ChannelSamples());
_frequencyDomain = SampleData.FreqDomain;

_synthesis.AddRange(DFT.Synthesize(_frequencyDomain));
}

this.MyModel = new PlotModel { Title = "Signal And Synthesis" };
this.MyModel.Series.Add(new FunctionSeries(getSynthesis, 0, 1000 - 1, 1.0, "Synthesis")); // Clip synthesis to signal sample count
this.MyModel.Series.Add(new FunctionSeries(getSignal, 0, 1000 - 1, 1.0, "Signal"));
this.MyModel.Series.Add(new FunctionSeries(getSynthesis, 0, _signal.Count - 1, 1.0, "Synthesis")); // Clip synthesis to signal sample count
// this.MyModel.Series.Add(new FunctionSeries(getSignal, 0, _signal.Count - 1, 1.0, "Signal"));

NotifyPropertyChanged("MyModel");
}
Expand Down Expand Up @@ -152,6 +159,23 @@ public String FilterType
}
}

public int FilterClipPercentage
{
get
{
return _filterClipPercentage;
}
set
{
_filterClipPercentage = value > 0 ? value : 0;
if (_filterClipPercentage > 100)
_filterClipPercentage = 100;

NotifyPropertyChanged();
GetNewModel();
}
}

public int CycleCount
{
get
Expand Down
6 changes: 3 additions & 3 deletions Signals And Transforms/View Models/HistogramViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public HistogramViewModel()
private void GetNewModel()
{

_signal = _signalSample.GetNextSamplesForTimeSlice(20);
_frequencyDomain = SampleData.FreqDomain;

MyModel = new PlotModel { Title = "Frequency Histogram" };
Expand All @@ -48,11 +47,12 @@ private void GetNewModel()
var linearAxis2 = new LinearAxis();
MyModel.Axes.Add(linearAxis2);
var linearBarSeries1 = new LinearBarSeries();
linearBarSeries1.Title = "Frequency";
linearBarSeries1.Title = "Hertz";

for (int idx = 0; idx < _frequencyDomain.RealComponent.Count; idx++)
{
linearBarSeries1.Points.Add(new DataPoint(idx, _frequencyDomain.ScaledRealComponent(idx)));
int hz = (int)Math.Floor((0.5) * ((float)idx / (float)(_frequencyDomain.RealComponent.Count - 1)) * SampleData.SignalSample.SampleRate);
linearBarSeries1.Points.Add(new DataPoint(hz, _frequencyDomain.Magnitude(idx)));
}

MyModel.Series.Add(linearBarSeries1);
Expand Down

0 comments on commit 96bc834

Please sign in to comment.